#define CYCastJSValue_(Type_) \
JSValueRef CYCastJSValue(JSContextRef context, Type_ value) { \
+ _assert(static_cast<Type_>(static_cast<double>(value)) == value); \
return JSValueMakeNumber(context, static_cast<double>(value)); \
}
return CYGetOffset(CYPoolCString(pool, context, value), index);
}
-void *CYCastPointer_(JSContextRef context, JSValueRef value) {
+void *CYCastPointer_(JSContextRef context, JSValueRef value, bool *guess) {
if (value == NULL)
return NULL;
else switch (JSValueGetType(context, value)) {
return NULL;
case kJSTypeObject: {
JSObjectRef object((JSObjectRef) value);
- if (JSValueIsObjectOfClass(context, value, CString_)) {
- CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(object)));
- return internal->value_;
- }
if (JSValueIsObjectOfClass(context, value, Pointer_)) {
Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
return internal->value_;
if (CYIsCallable(context, toPointer)) {
JSValueRef value(CYCallAsFunction(context, (JSObjectRef) toPointer, object, 0, NULL));
_assert(value != NULL);
- return CYCastPointer_(context, value);
+ return CYCastPointer_(context, value, guess);
}
} default:
+ if (guess != NULL)
+ *guess = true;
+ case kJSTypeNumber:
double number(CYCastDouble(context, value));
- if (std::isnan(number))
+ if (!std::isnan(number))
+ return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
+ if (guess == NULL)
throw CYJSError(context, "cannot convert value to pointer");
- return reinterpret_cast<void *>(static_cast<uintptr_t>(static_cast<long long>(number)));
+ else {
+ *guess = true;
+ return NULL;
+ }
}
}
*reinterpret_cast<void **>(data) = CYCastPointer<void *>(context, value);
break;
- case sig::string_P:
- _assert(pool != NULL);
- *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value);
- break;
+ case sig::string_P: {
+ bool guess(false);
+ *reinterpret_cast<const char **>(data) = CYCastPointer<const char *>(context, value, &guess);
+ if (guess && pool != NULL)
+ *reinterpret_cast<const char **>(data) = CYPoolCString(*pool, context, value);
+ } break;
case sig::struct_P: {
uint8_t *base(reinterpret_cast<uint8_t *>(data));
return CYCallFunction(pool, context, 0, NULL, count, arguments, false, &internal->signature_, &internal->cif_, internal->GetValue());
} CYCatch(NULL) }
+static JSValueRef Pointer_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ Pointer *internal(reinterpret_cast<Pointer *>(JSObjectGetPrivate(object)));
+ if (internal->type_->type_->primitive != sig::function_P)
+ throw CYJSError(context, "cannot call a pointer to non-function");
+ JSObjectRef functor(CYCastJSObject(context, CYGetProperty(context, object, cyi_s)));
+ return CYCallAsFunction(context, functor, _this, count, arguments);
+} CYCatch(NULL) }
+
JSObjectRef CYMakeType(JSContextRef context, const char *encoding) {
Type_privateData *internal(new Type_privateData(encoding));
return JSObjectMake(context, Type_privateData::Class_, internal);
return CYMakeFunctor(context, arguments[0], signature);
} CYCatch(NULL) }
+static JSValueRef CString_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CString *internal(reinterpret_cast<CString *>(JSObjectGetPrivate(_this)));
+
+ sig::Type type;
+ type.name = NULL;
+ type.flags = 0;
+
+ type.primitive = sig::char_P;
+ type.data.data.type = NULL;
+ type.data.data.size = 0;
+
+ return CYMakePointer(context, internal->value_, _not(size_t), &type, NULL, NULL);
+} CYCatch(NULL) }
+
+static JSValueRef Functor_callAsFunction_$cya(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ CYPool pool;
+ cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(_this)));
+
+ sig::Type type;
+ type.name = NULL;
+ type.flags = 0;
+
+ type.primitive = sig::function_P;
+ sig::Copy(pool, type.data.signature, internal->signature_);
+
+ return CYMakePointer(context, internal->value_, _not(size_t), &type, NULL, NULL);
+} CYCatch(NULL) }
+
+static JSValueRef Pointer_callAsFunction_toPointer(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ return _this;
+} CYCatch(NULL) }
+
static JSValueRef CYValue_callAsFunction_valueOf(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
return CYCastJSValue(context, reinterpret_cast<uintptr_t>(internal->value_));
static JSValueRef CYValue_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
CYValue *internal(reinterpret_cast<CYValue *>(JSObjectGetPrivate(_this)));
- char string[32];
- sprintf(string, "%p", internal->value_);
- return CYCastJSValue(context, string);
+ std::ostringstream str;
+ Dl_info info;
+ if (internal->value_ == NULL)
+ str << "NULL";
+ else if (dladdr(internal->value_, &info) == 0)
+ str << internal->value_;
+ else {
+ str << info.dli_sname;
+ off_t offset(static_cast<char *>(internal->value_) - static_cast<char *>(info.dli_saddr));
+ if (offset != 0)
+ str << "+0x" << std::hex << offset;
+ }
+ std::string value(str.str());
+ return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
static JSValueRef Pointer_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
const char *string(static_cast<const char *>(internal->value_));
std::ostringstream str;
str << "&";
- CYStringify(str, string, strlen(string));
+ CYStringify(str, string, strlen(string), true);
std::string value(str.str());
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
return Type_callAsFunction_toString(context, object, _this, count, arguments, exception);
}
-static JSStaticFunction CString_staticFunctions[5] = {
+static JSStaticFunction CString_staticFunctions[6] = {
{"toCYON", &CString_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toPointer", &CString_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toString", &CString_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"valueOf", &CString_callAsFunction_toString, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL, 0}
{NULL, NULL, NULL, 0}
};
-static JSStaticFunction Pointer_staticFunctions[4] = {
+static JSStaticFunction Pointer_staticFunctions[5] = {
{"toCYON", &Pointer_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
+ {"toPointer", &Pointer_callAsFunction_toPointer, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{NULL, NULL, 0}
};
{NULL, NULL, 0}
};
-static JSStaticFunction Functor_staticFunctions[4] = {
+static JSStaticFunction Functor_staticFunctions[5] = {
+ {"$cya", &Functor_callAsFunction_$cya, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toCYON", &CYValue_callAsFunction_toCYON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"toJSON", &CYValue_callAsFunction_toJSON, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
{"valueOf", &CYValue_callAsFunction_valueOf, kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete},
definition.className = "Pointer";
definition.staticFunctions = Pointer_staticFunctions;
definition.staticValues = Pointer_staticValues;
+ definition.callAsFunction = &Pointer_callAsFunction;
definition.getProperty = &Pointer_getProperty;
definition.setProperty = &Pointer_setProperty;
definition.finalize = &CYFinalize;
CYCallAsFunction(context, function, NULL, 3, arguments);
}
- return CYGetProperty(context, module, property);
+ JSObjectRef exports(CYCastJSObject(context, CYGetProperty(context, module, property)));
+
+ CYJSString _default("default");
+ if (JSValueIsUndefined(context, CYGetProperty(context, exports, _default)))
+ CYSetProperty(context, exports, _default, exports, kJSPropertyAttributeDontEnum);
+
+ return exports;
} CYCatch(NULL) }
static bool CYRunScript(JSGlobalContextRef context, const char *path) {