return CYJSUndefined(context);
} CYCatch(NULL) }
+static JSValueRef Global_print(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
+ FILE *file(stdout);
+ CYPool pool;
+
+ for (size_t i(0); i != count; ++i) {
+ if (i != 0)
+ fputc(' ', file);
+ CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, arguments[i])));
+ fwrite(string.data, string.size, 1, file);
+ }
+
+ fputc('\n', file);
+ fflush(file);
+ return CYJSUndefined(context);
+} CYCatch(NULL) }
+
static void (*JSSynchronousGarbageCollectForDebugging$)(JSContextRef);
_visible void CYGarbageCollect(JSContextRef context) {
case kJSTypeString: {
std::ostringstream str;
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, value)));
- CYStringify(str, string.data, string.size);
+ CYStringify(str, string.data, string.size, CYStringifyModeCycript);
std::string value(str.str());
return pool.strmemdup(value.c_str(), value.size());
} break;
if (CYIsKey(string))
str << string.data;
else
- CYStringify(str, string.data, string.size);
+ CYStringify(str, string.data, string.size, CYStringifyModeLegacy);
str << ':';
std::ostringstream str;
CYUTF8String string(CYPoolUTF8String(pool, context, CYJSString(context, _this)));
- CYStringify(str, string.data, string.size);
+ CYStringify(str, string.data, string.size, CYStringifyModeCycript);
std::string value(str.str());
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
} CYCatch(NULL) }
static JSValueRef All_complete_callAsFunction(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
- _assert(count == 1);
+ _assert(count == 1 || count == 2);
CYPool pool;
CYUTF8String prefix(CYPoolUTF8String(pool, context, CYJSString(context, arguments[0])));
static JSValueRef Functor_callAsFunction_toCYON(JSContextRef context, JSObjectRef object, JSObjectRef _this, size_t count, const JSValueRef arguments[], JSValueRef *exception) { CYTry {
cy::Functor *internal(reinterpret_cast<cy::Functor *>(JSObjectGetPrivate(_this)));
uint8_t *value(reinterpret_cast<uint8_t *>(internal->value_));
+ _assert(value != NULL);
CYLocalPool pool;
sig::Function function(internal->variadic_);
sig::Copy(pool, function.signature, internal->signature_);
- CYString *name;
+ CYPropertyName *name;
auto typed(CYDecodeType(pool, &function)); {
std::ostringstream str;
Dl_info info;
- if (internal->value_ == NULL)
- str << "NULL";
- else if (dladdr(value, &info) == 0)
- str << internal->value_;
- else {
+ if (dladdr(value, &info) == 0) {
+ str << (void *) value;
+ name = new(pool) CYNumber(reinterpret_cast<uintptr_t>(value));
+ } else {
str << info.dli_sname;
off_t offset(value - reinterpret_cast<uint8_t *>(info.dli_saddr));
if (offset != 0)
str << "+0x" << std::hex << offset;
+ name = new(pool) CYString(pool.strdup(str.str().c_str()));
}
-
- name = new(pool) CYString(pool.strdup(str.str().c_str()));
}
std::ostringstream str;
str << "NULL";
else {
str << "&";
- CYStringify(str, string, strlen(string), true);
+ CYStringify(str, string, strlen(string), CYStringifyModeNative);
}
std::string value(str.str());
return CYCastJSValue(context, CYJSString(CYUTF8String(value.c_str(), value.size())));
{NULL, NULL, 0}
};
-_visible void CYSetArgs(int argc, const char *argv[]) {
+_visible void CYSetArgs(const char *argv0, const char *script, int argc, const char *argv[]) {
JSContextRef context(CYGetJSContext());
- JSValueRef args[argc];
+ JSValueRef args[argc + 2];
for (int i(0); i != argc; ++i)
- args[i] = CYCastJSValue(context, argv[i]);
+ args[i + 2] = CYCastJSValue(context, argv[i]);
+
+ size_t offset;
+ if (script == NULL)
+ offset = 1;
+ else {
+ offset = 0;
+ args[1] = CYCastJSValue(context, CYJSString(script));
+ }
- JSObjectRef array(CYObjectMakeArray(context, argc, args));
- JSObjectRef System(CYGetCachedObject(context, CYJSString("System")));
- CYSetProperty(context, System, CYJSString("args"), array);
+ args[offset] = CYCastJSValue(context, CYJSString(argv0));
+
+ CYSetProperty(context, CYGetCachedObject(context, CYJSString("System")), CYJSString("args"), CYObjectMakeArray(context, argc, args + 2));
+ CYSetProperty(context, CYGetCachedObject(context, CYJSString("process")), CYJSString("argv"), CYObjectMakeArray(context, argc + 2 - offset, args + offset));
}
JSObjectRef CYGetGlobalObject(JSContextRef context) {
CYSetPrototype(context, last, cache);
}
- JSObjectRef System(JSObjectMake(context, NULL, NULL));
- CYSetProperty(context, cy, CYJSString("System"), System);
-
- CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum);
-
- CYSetProperty(context, global, CYJSString("system"), System);
- CYSetProperty(context, System, CYJSString("args"), CYJSNull(context));
- CYSetProperty(context, System, CYJSString("print"), &System_print);
-
- CYSetProperty(context, global, CYJSString("global"), global);
-
#ifdef __APPLE__
if (&JSWeakObjectMapCreate != NULL) {
JSWeakObjectMapRef weak(JSWeakObjectMapCreate(context, NULL, &CYDestroyWeak));
CYSetProperty(context, cache, CYJSString("float"), CYMakeType(context, sig::Primitive<float>()), kJSPropertyAttributeDontEnum);
CYSetProperty(context, cache, CYJSString("double"), CYMakeType(context, sig::Primitive<double>()), kJSPropertyAttributeDontEnum);
+ CYSetProperty(context, global, CYJSString("require"), &require_callAsFunction, kJSPropertyAttributeDontEnum);
+
+ JSObjectRef System(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, all, CYJSString("system"), System);
+ System = CYCastJSObject(context, CYGetProperty(context, global, CYJSString("system")));
+ CYSetProperty(context, cy, CYJSString("System"), System);
+
+ JSObjectRef process(JSObjectMake(context, NULL, NULL));
+ CYSetProperty(context, global, CYJSString("process"), process);
+ CYSetProperty(context, cy, CYJSString("process"), process);
+
+ CYSetProperty(context, System, CYJSString("args"), CYJSNull(context));
+ CYSetProperty(context, System, CYJSString("print"), &System_print);
+
+ CYSetProperty(context, global, CYJSString("global"), global);
+ CYSetProperty(context, global, CYJSString("print"), &Global_print);
+
for (CYHook *hook : GetHooks())
if (hook->SetupContext != NULL)
(*hook->SetupContext)(context);