void JSCExecutor::initOnJSVMThread() { m_context = JSGlobalContextCreateInGroup(nullptr, nullptr); s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativePerformanceNow", nativePerformanceNow); installGlobalFunction(m_context, "nativeStartWorker", nativeStartWorker); installGlobalFunction(m_context, "nativePostMessageToWorker", nativePostMessageToWorker); installGlobalFunction(m_context, "nativeTerminateWorker", nativeTerminateWorker); installGlobalFunction(m_context, "nativeInjectHMRUpdate", nativeInjectHMRUpdate); installGlobalFunction(m_context, "nativeLoggingHook", JSLogging::nativeHook); // TODO (t10136849): Pass the config options from map to JSC #ifdef WITH_FB_JSC_TUNING configureJSCForAndroid(); #endif #ifdef WITH_JSC_EXTRA_TRACING addNativeTracingHooks(m_context); addNativeProfilingHooks(m_context); PerfLogging::installNativeHooks(m_context); #endif #ifdef WITH_FB_MEMORY_PROFILING addNativeMemoryHooks(m_context); #endif }
void JSCExecutor::initOnJSVMThread() { #if defined(WITH_FB_JSC_TUNING) configureJSCForAndroid(m_jscConfig); #endif m_context = JSGlobalContextCreateInGroup(nullptr, nullptr); s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativeStartWorker", nativeStartWorker); installGlobalFunction(m_context, "nativePostMessageToWorker", nativePostMessageToWorker); installGlobalFunction(m_context, "nativeTerminateWorker", nativeTerminateWorker); installGlobalFunction(m_context, "nativeInjectHMRUpdate", nativeInjectHMRUpdate); installGlobalFunction(m_context, "nativeCallSyncHook", nativeCallSyncHook); installGlobalFunction(m_context, "nativeLoggingHook", JSNativeHooks::loggingHook); installGlobalFunction(m_context, "nativePerformanceNow", JSNativeHooks::nowHook); #ifdef WITH_JSC_EXTRA_TRACING addNativeTracingHooks(m_context); addNativeProfilingHooks(m_context); PerfLogging::installNativeHooks(m_context); #endif #ifdef WITH_FB_MEMORY_PROFILING addNativeMemoryHooks(m_context); #endif #ifdef JSC_HAS_PERF_STATS_API addJSCPerfStatsHooks(m_context); #endif #if defined(WITH_FB_JSC_TUNING) configureJSContextForAndroid(m_context, m_jscConfig, m_deviceCacheDir); #endif }
JSCExecutor::JSCExecutor(FlushImmediateCallback cb, const std::string& cacheDir) : m_flushImmediateCallback(cb), m_deviceCacheDir(cacheDir) { m_context = JSGlobalContextCreateInGroup(nullptr, nullptr); m_messageQueueThread = MessageQueues::getCurrentMessageQueueThread(); s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativePerformanceNow", nativePerformanceNow); installGlobalFunction(m_context, "nativeStartWorker", nativeStartWorker); installGlobalFunction(m_context, "nativePostMessageToWorker", nativePostMessageToWorker); installGlobalFunction(m_context, "nativeTerminateWorker", nativeTerminateWorker); installGlobalFunction(m_context, "nativeLoggingHook", JSLogging::nativeHook); #ifdef WITH_FB_JSC_TUNING configureJSCForAndroid(); #endif #ifdef WITH_JSC_EXTRA_TRACING addNativeTracingHooks(m_context); addNativeProfilingHooks(m_context); PerfLogging::installNativeHooks(m_context); #endif #ifdef WITH_FB_MEMORY_PROFILING addNativeMemoryHooks(m_context); #endif }
void JSCExecutor::initOnJSVMThread() { #if defined(WITH_FB_JSC_TUNING) && !defined(WITH_JSC_INTERNAL) // TODO: Find a way to pass m_jscConfig to configureJSCForAndroid() configureJSCForAndroid(m_jscConfig.getDefault("GCTimers", false).asBool()); #endif m_context = JSGlobalContextCreateInGroup(nullptr, nullptr); s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativePerformanceNow", nativePerformanceNow); installGlobalFunction(m_context, "nativeStartWorker", nativeStartWorker); installGlobalFunction(m_context, "nativePostMessageToWorker", nativePostMessageToWorker); installGlobalFunction(m_context, "nativeTerminateWorker", nativeTerminateWorker); installGlobalFunction(m_context, "nativeInjectHMRUpdate", nativeInjectHMRUpdate); installGlobalFunction(m_context, "nativeLoggingHook", JSLogging::nativeHook); #if defined(WITH_JSC_INTERNAL) && defined(WITH_FB_JSC_TUNING) configureJSCForAndroid(); #endif #ifdef WITH_JSC_EXTRA_TRACING addNativeTracingHooks(m_context); addNativeProfilingHooks(m_context); PerfLogging::installNativeHooks(m_context); #endif #ifdef WITH_FB_MEMORY_PROFILING addNativeMemoryHooks(m_context); #endif #ifdef JSC_HAS_PERF_STATS_API addJSCPerfStatsHooks(m_context); #endif }
gchar* sokoke_js_script_eval (JSContextRef js_context, const gchar* script, gchar** exception) { #if 0 //CMP_ERROR_TODO implement JSGlobalContextRef temporary_context = NULL; gchar* value; JSStringRef js_value_string; JSStringRef js_script; JSValueRef js_exception = NULL; JSValueRef js_value; g_return_val_if_fail (script, FALSE); if (!js_context) js_context = temporary_context = JSGlobalContextCreateInGroup (NULL, NULL); js_script = JSStringCreateWithUTF8CString (script); js_value = JSEvaluateScript (js_context, js_script, JSContextGetGlobalObject (js_context), NULL, 0, &js_exception); JSStringRelease (js_script); if (!js_value) { JSStringRef js_message = JSValueToStringCopy (js_context, js_exception, NULL); g_return_val_if_fail (js_message != NULL, NULL); value = sokoke_js_string_utf8 (js_message); if (exception) *exception = value; else { g_warning ("%s", value); g_free (value); } JSStringRelease (js_message); if (temporary_context) JSGlobalContextRelease (temporary_context); return NULL; } js_value_string = JSValueToStringCopy (js_context, js_value, NULL); value = sokoke_js_string_utf8 (js_value_string); JSStringRelease (js_value_string); if (temporary_context) JSGlobalContextRelease (temporary_context); return value; #else return 0; #endif }
/* static */ VJSGlobalContext *VJSGlobalContext::Create( IJSRuntimeDelegate *inRuntimeDelegate, JS4D::ClassRef inGlobalClassRef) { JSGlobalContextRef ref = JSGlobalContextCreateInGroup( NULL, inGlobalClassRef); if (ref != NULL) { VJSGlobalObject *globalObject = new VJSGlobalObject( ref, NULL, inRuntimeDelegate); JSObjectSetPrivate( JSContextGetGlobalObject( ref), globalObject); return new VJSGlobalContext( ref); } else return NULL; }
void JSCWebWorker::initJSVMAndLoadScript() { CHECK(!isTerminated()) << "Worker was already finished!"; CHECK(!context_) << "Worker JS VM was already created!"; context_ = JSGlobalContextCreateInGroup( NULL, // use default JS 'global' object NULL // create new group (i.e. new VM) ); s_globalContextRefToJSCWebWorker[context_] = this; // TODO(9604438): Protect against script does not exist std::unique_ptr<const JSBigString> script = WebWorkerUtil::loadScriptFromAssets(scriptName_); evaluateScript(context_, jsStringFromBigString(*script), String(scriptName_.c_str())); installGlobalFunction(context_, "postMessage", nativePostMessage); }
void JSCWebWorker::initJSVMAndLoadScript() { FBASSERTMSGF(!isTerminated(), "Worker was already finished!"); FBASSERTMSGF(!context_, "Worker JS VM was already created!"); context_ = JSGlobalContextCreateInGroup( NULL, // use default JS 'global' object NULL // create new group (i.e. new VM) ); s_globalContextRefToJSCWebWorker[context_] = this; // TODO(9604438): Protect against script does not exist std::string script = loadScriptFromAssets(scriptName_); evaluateScript(context_, String(script.c_str()), String(scriptName_.c_str())); installGlobalFunction(context_, "postMessage", nativePostMessage); }
JSCExecutor::JSCExecutor(FlushImmediateCallback cb) : m_flushImmediateCallback(cb) { m_context = JSGlobalContextCreateInGroup(nullptr, nullptr); s_globalContextRefToJSCExecutor[m_context] = this; installGlobalFunction(m_context, "nativeFlushQueueImmediate", nativeFlushQueueImmediate); installGlobalFunction(m_context, "nativeLoggingHook", nativeLoggingHook); #ifdef WITH_FB_JSC_TUNING configureJSCForAndroid(); #endif #ifdef WITH_JSC_EXTRA_TRACING addNativeTracingHooks(m_context); addNativeProfilingHooks(m_context); addNativePerfLoggingHooks(m_context); #endif }
static void InitializeHyperloopVM() #endif { globalContextGroupRef = JSContextGroupCreate(); #ifdef USE_TIJSCORE globalContextRef = ctx; #else JSContextGroupRetain(globalContextGroupRef); globalContextRef = JSGlobalContextCreateInGroup(globalContextGroupRef,nullptr); #endif // initialize InitializeContext(globalContextRef); // retain it JSGlobalContextRetain(globalContextRef); }
int testGlobalContextWithFinalizer() { JSClassDefinition def = kJSClassDefinitionEmpty; def.className = "testClass"; def.finalize = finalize; JSClassRef classRef = JSClassCreate(&def); JSGlobalContextRef ref = JSGlobalContextCreateInGroup(nullptr, classRef); JSGlobalContextRelease(ref); JSClassRelease(classRef); if (failed) printf("FAIL: JSGlobalContextRef did not call its JSClassRef finalizer.\n"); else printf("PASS: JSGlobalContextRef called its JSClassRef finalizer as expected.\n"); return failed; }
/** * run JS in a new context and return result */ static JSValueRef RunInNewContext(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { if (argumentCount > 0) { std::ostringstream stream; std::string js(HyperloopJSValueToStringCopy(ctx,arguments[0],exception)); stream << "(function(){" << js << "})"; auto newCtx = JSGlobalContextCreateInGroup(globalContextGroupRef,nullptr); auto scriptRef = JSStringCreateWithUTF8CString(stream.str().c_str()); auto thisObjectRef = argumentCount > 1 ? JSValueToObject(ctx,arguments[1],exception) : thisObject; auto functionRef = JSEvaluateScript(newCtx,scriptRef,thisObjectRef,nullptr,0,exception); auto functionObj = JSValueToObject(newCtx,functionRef,exception); auto resultRef = JSObjectCallAsFunction(newCtx,functionObj,thisObjectRef,0,nullptr,exception); JSStringRelease(scriptRef); JSGlobalContextRelease(newCtx); return resultRef; } return JSValueMakeUndefined(ctx); }
int main(int argc, char* argv[]) { const char *scriptPath = "minidom.js"; if (argc > 1) { scriptPath = argv[1]; } JSGlobalContextRef context = JSGlobalContextCreateInGroup(NULL, NULL); JSObjectRef globalObject = JSContextGetGlobalObject(context); JSStringRef printIString = JSStringCreateWithUTF8CString("print"); JSObjectSetProperty(context, globalObject, printIString, JSObjectMakeFunctionWithCallback(context, printIString, print), kJSPropertyAttributeNone, NULL); JSStringRelease(printIString); JSStringRef node = JSStringCreateWithUTF8CString("Node"); JSObjectSetProperty(context, globalObject, node, JSObjectMakeConstructor(context, JSNode_class(context), JSNode_construct), kJSPropertyAttributeNone, NULL); JSStringRelease(node); char* scriptUTF8 = createStringWithContentsOfFile(scriptPath); JSStringRef script = JSStringCreateWithUTF8CString(scriptUTF8); JSValueRef exception; JSValueRef result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception); if (result) printf("PASS: Test script executed successfully.\n"); else { printf("FAIL: Test script threw exception:\n"); JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL); size_t exceptionUTF8Size = JSStringGetMaximumUTF8CStringSize(exceptionIString); char* exceptionUTF8 = (char*)malloc(exceptionUTF8Size); JSStringGetUTF8CString(exceptionIString, exceptionUTF8, exceptionUTF8Size); printf("%s\n", exceptionUTF8); free(exceptionUTF8); JSStringRelease(exceptionIString); } JSStringRelease(script); free(scriptUTF8); globalObject = 0; JSGlobalContextRelease(context); printf("PASS: Program exited normally.\n"); return 0; }
/** * seed_context_create: * @group: A #SeedContextGroup in which to create the new context, or %NULL to * create it in the default context group. * @global_class: The #SeedClass to use to create the global object, or %NULL to * create it with the default class. * * Create a new #SeedContext. By default, this creates a new context * which has no global objects; you can add the default set using * seed_prepare_global_context(). * * Return value: A new #SeedContext. * */ JSGlobalContextRef seed_context_create(JSContextGroupRef group, JSClassRef global_class) { return JSGlobalContextCreateInGroup(group, global_class); }
void seed_signal_marshal_func (GClosure * closure, GValue * return_value, guint n_param_values, const GValue * param_values, gpointer invocation_hint, gpointer marshal_data) { SeedClosure *seed_closure = (SeedClosure *) closure; JSValueRef *args, exception = 0; JSValueRef ret = 0; guint i; gchar *mes; GSignalQuery signal_query = { 0, }; if (marshal_data) { /* Inspired from gjs/gi/value.c:closure_marshal() */ /* we are used for a signal handler */ guint signal_id; signal_id = GPOINTER_TO_UINT(marshal_data); g_signal_query(signal_id, &signal_query); if (!signal_query.signal_id) g_error("Signal handler being called on invalid signal"); if (signal_query.n_params + 1 != n_param_values) g_error("Signal handler being called with wrong number of parameters"); } JSContextRef ctx = JSGlobalContextCreateInGroup (context_group, 0); seed_prepare_global_context (ctx); SEED_NOTE (INVOCATION, "Signal Marshal: "); args = g_newa (JSValueRef, n_param_values + 1); for (i = 0; i < n_param_values; i++) { args[i] = seed_value_from_gvalue_for_signal (ctx, (GValue *) & param_values[i], 0, &signal_query, i); if (!args[i]) g_error ("Error in signal marshal. " "Unable to convert argument of type: %s \n", g_type_name (param_values[i].g_type)); } if (seed_closure->user_data) args[i] = seed_closure->user_data; else args[i] = JSValueMakeNull (ctx); ret = JSObjectCallAsFunction (ctx, seed_closure->function, NULL, n_param_values + 1, args, &exception); if (exception) { seed_closure_warn_exception (closure, ctx, exception); exception = NULL; } if (ret && !JSValueIsNull (ctx, ret) && (seed_closure->return_type != G_TYPE_NONE)) { seed_value_to_gvalue (ctx, ret, seed_closure->return_type, return_value, &exception); } if (exception) { mes = seed_exception_to_string (ctx, exception); g_warning ("Exception in signal handler return value. %s \n", mes); g_free (mes); } JSGlobalContextRelease ((JSGlobalContextRef) ctx); JSGarbageCollect(ctx); }
int main(int argc, char* argv[]) { const char *scriptPath = "testapi.js"; if (argc > 1) { scriptPath = argv[1]; } // Test garbage collection with a fresh context context = JSGlobalContextCreateInGroup(NULL, NULL); TestInitializeFinalize = true; testInitializeFinalize(); JSGlobalContextRelease(context); TestInitializeFinalize = false; ASSERT(Base_didFinalize); JSClassDefinition globalObjectClassDefinition = kJSClassDefinitionEmpty; globalObjectClassDefinition.initialize = globalObject_initialize; globalObjectClassDefinition.staticValues = globalObject_staticValues; globalObjectClassDefinition.staticFunctions = globalObject_staticFunctions; globalObjectClassDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; JSClassRef globalObjectClass = JSClassCreate(&globalObjectClassDefinition); context = JSGlobalContextCreateInGroup(NULL, globalObjectClass); JSGlobalContextRetain(context); JSGlobalContextRelease(context); JSObjectRef globalObject = JSContextGetGlobalObject(context); ASSERT(JSValueIsObject(context, globalObject)); JSValueRef jsUndefined = JSValueMakeUndefined(context); JSValueRef jsNull = JSValueMakeNull(context); JSValueRef jsTrue = JSValueMakeBoolean(context, true); JSValueRef jsFalse = JSValueMakeBoolean(context, false); JSValueRef jsZero = JSValueMakeNumber(context, 0); JSValueRef jsOne = JSValueMakeNumber(context, 1); JSValueRef jsOneThird = JSValueMakeNumber(context, 1.0 / 3.0); JSObjectRef jsObjectNoProto = JSObjectMake(context, NULL, NULL); JSObjectSetPrototype(context, jsObjectNoProto, JSValueMakeNull(context)); // FIXME: test funny utf8 characters JSStringRef jsEmptyIString = JSStringCreateWithUTF8CString(""); JSValueRef jsEmptyString = JSValueMakeString(context, jsEmptyIString); JSStringRef jsOneIString = JSStringCreateWithUTF8CString("1"); JSValueRef jsOneString = JSValueMakeString(context, jsOneIString); UniChar singleUniChar = 65; // Capital A CFMutableStringRef cfString = CFStringCreateMutableWithExternalCharactersNoCopy(kCFAllocatorDefault, &singleUniChar, 1, 1, kCFAllocatorNull); JSStringRef jsCFIString = JSStringCreateWithCFString(cfString); JSValueRef jsCFString = JSValueMakeString(context, jsCFIString); CFStringRef cfEmptyString = CFStringCreateWithCString(kCFAllocatorDefault, "", kCFStringEncodingUTF8); JSStringRef jsCFEmptyIString = JSStringCreateWithCFString(cfEmptyString); JSValueRef jsCFEmptyString = JSValueMakeString(context, jsCFEmptyIString); CFIndex cfStringLength = CFStringGetLength(cfString); UniChar* buffer = (UniChar*)malloc(cfStringLength * sizeof(UniChar)); CFStringGetCharacters(cfString, CFRangeMake(0, cfStringLength), buffer); JSStringRef jsCFIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, cfStringLength); JSValueRef jsCFStringWithCharacters = JSValueMakeString(context, jsCFIStringWithCharacters); JSStringRef jsCFEmptyIStringWithCharacters = JSStringCreateWithCharacters((JSChar*)buffer, CFStringGetLength(cfEmptyString)); free(buffer); JSValueRef jsCFEmptyStringWithCharacters = JSValueMakeString(context, jsCFEmptyIStringWithCharacters); ASSERT(JSValueGetType(context, jsUndefined) == kJSTypeUndefined); ASSERT(JSValueGetType(context, jsNull) == kJSTypeNull); ASSERT(JSValueGetType(context, jsTrue) == kJSTypeBoolean); ASSERT(JSValueGetType(context, jsFalse) == kJSTypeBoolean); ASSERT(JSValueGetType(context, jsZero) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsOne) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsOneThird) == kJSTypeNumber); ASSERT(JSValueGetType(context, jsEmptyString) == kJSTypeString); ASSERT(JSValueGetType(context, jsOneString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFStringWithCharacters) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFEmptyString) == kJSTypeString); ASSERT(JSValueGetType(context, jsCFEmptyStringWithCharacters) == kJSTypeString); JSObjectRef myObject = JSObjectMake(context, MyObject_class(context), NULL); JSStringRef myObjectIString = JSStringCreateWithUTF8CString("MyObject"); JSObjectSetProperty(context, globalObject, myObjectIString, myObject, kJSPropertyAttributeNone, NULL); JSStringRelease(myObjectIString); JSValueRef exception; // Conversions that throw exceptions exception = NULL; ASSERT(NULL == JSValueToObject(context, jsNull, &exception)); ASSERT(exception); exception = NULL; // FIXME <rdar://4668451> - On i386 the isnan(double) macro tries to map to the isnan(float) function, // causing a build break with -Wshorten-64-to-32 enabled. The issue is known by the appropriate team. // After that's resolved, we can remove these casts ASSERT(isnan((float)JSValueToNumber(context, jsObjectNoProto, &exception))); ASSERT(exception); exception = NULL; ASSERT(!JSValueToStringCopy(context, jsObjectNoProto, &exception)); ASSERT(exception); ASSERT(JSValueToBoolean(context, myObject)); exception = NULL; ASSERT(!JSValueIsEqual(context, jsObjectNoProto, JSValueMakeNumber(context, 1), &exception)); ASSERT(exception); exception = NULL; JSObjectGetPropertyAtIndex(context, myObject, 0, &exception); ASSERT(1 == JSValueToNumber(context, exception, NULL)); assertEqualsAsBoolean(jsUndefined, false); assertEqualsAsBoolean(jsNull, false); assertEqualsAsBoolean(jsTrue, true); assertEqualsAsBoolean(jsFalse, false); assertEqualsAsBoolean(jsZero, false); assertEqualsAsBoolean(jsOne, true); assertEqualsAsBoolean(jsOneThird, true); assertEqualsAsBoolean(jsEmptyString, false); assertEqualsAsBoolean(jsOneString, true); assertEqualsAsBoolean(jsCFString, true); assertEqualsAsBoolean(jsCFStringWithCharacters, true); assertEqualsAsBoolean(jsCFEmptyString, false); assertEqualsAsBoolean(jsCFEmptyStringWithCharacters, false); assertEqualsAsNumber(jsUndefined, nan("")); assertEqualsAsNumber(jsNull, 0); assertEqualsAsNumber(jsTrue, 1); assertEqualsAsNumber(jsFalse, 0); assertEqualsAsNumber(jsZero, 0); assertEqualsAsNumber(jsOne, 1); assertEqualsAsNumber(jsOneThird, 1.0 / 3.0); assertEqualsAsNumber(jsEmptyString, 0); assertEqualsAsNumber(jsOneString, 1); assertEqualsAsNumber(jsCFString, nan("")); assertEqualsAsNumber(jsCFStringWithCharacters, nan("")); assertEqualsAsNumber(jsCFEmptyString, 0); assertEqualsAsNumber(jsCFEmptyStringWithCharacters, 0); ASSERT(sizeof(JSChar) == sizeof(UniChar)); assertEqualsAsCharactersPtr(jsUndefined, "undefined"); assertEqualsAsCharactersPtr(jsNull, "null"); assertEqualsAsCharactersPtr(jsTrue, "true"); assertEqualsAsCharactersPtr(jsFalse, "false"); assertEqualsAsCharactersPtr(jsZero, "0"); assertEqualsAsCharactersPtr(jsOne, "1"); assertEqualsAsCharactersPtr(jsOneThird, "0.3333333333333333"); assertEqualsAsCharactersPtr(jsEmptyString, ""); assertEqualsAsCharactersPtr(jsOneString, "1"); assertEqualsAsCharactersPtr(jsCFString, "A"); assertEqualsAsCharactersPtr(jsCFStringWithCharacters, "A"); assertEqualsAsCharactersPtr(jsCFEmptyString, ""); assertEqualsAsCharactersPtr(jsCFEmptyStringWithCharacters, ""); assertEqualsAsUTF8String(jsUndefined, "undefined"); assertEqualsAsUTF8String(jsNull, "null"); assertEqualsAsUTF8String(jsTrue, "true"); assertEqualsAsUTF8String(jsFalse, "false"); assertEqualsAsUTF8String(jsZero, "0"); assertEqualsAsUTF8String(jsOne, "1"); assertEqualsAsUTF8String(jsOneThird, "0.3333333333333333"); assertEqualsAsUTF8String(jsEmptyString, ""); assertEqualsAsUTF8String(jsOneString, "1"); assertEqualsAsUTF8String(jsCFString, "A"); assertEqualsAsUTF8String(jsCFStringWithCharacters, "A"); assertEqualsAsUTF8String(jsCFEmptyString, ""); assertEqualsAsUTF8String(jsCFEmptyStringWithCharacters, ""); ASSERT(JSValueIsStrictEqual(context, jsTrue, jsTrue)); ASSERT(!JSValueIsStrictEqual(context, jsOne, jsOneString)); ASSERT(JSValueIsEqual(context, jsOne, jsOneString, NULL)); ASSERT(!JSValueIsEqual(context, jsTrue, jsFalse, NULL)); CFStringRef cfJSString = JSStringCopyCFString(kCFAllocatorDefault, jsCFIString); CFStringRef cfJSEmptyString = JSStringCopyCFString(kCFAllocatorDefault, jsCFEmptyIString); ASSERT(CFEqual(cfJSString, cfString)); ASSERT(CFEqual(cfJSEmptyString, cfEmptyString)); CFRelease(cfJSString); CFRelease(cfJSEmptyString); CFRelease(cfString); CFRelease(cfEmptyString); jsGlobalValue = JSObjectMake(context, NULL, NULL); JSValueProtect(context, jsGlobalValue); JSGarbageCollect(context); ASSERT(JSValueIsObject(context, jsGlobalValue)); JSValueUnprotect(context, jsGlobalValue); JSStringRef goodSyntax = JSStringCreateWithUTF8CString("x = 1;"); JSStringRef badSyntax = JSStringCreateWithUTF8CString("x := 1;"); ASSERT(JSCheckScriptSyntax(context, goodSyntax, NULL, 0, NULL)); ASSERT(!JSCheckScriptSyntax(context, badSyntax, NULL, 0, NULL)); JSValueRef result; JSValueRef v; JSObjectRef o; JSStringRef string; result = JSEvaluateScript(context, goodSyntax, NULL, NULL, 1, NULL); ASSERT(result); ASSERT(JSValueIsEqual(context, result, jsOne, NULL)); exception = NULL; result = JSEvaluateScript(context, badSyntax, NULL, NULL, 1, &exception); ASSERT(!result); ASSERT(JSValueIsObject(context, exception)); JSStringRef array = JSStringCreateWithUTF8CString("Array"); JSObjectRef arrayConstructor = JSValueToObject(context, JSObjectGetProperty(context, globalObject, array, NULL), NULL); JSStringRelease(array); result = JSObjectCallAsConstructor(context, arrayConstructor, 0, NULL, NULL); ASSERT(result); ASSERT(JSValueIsObject(context, result)); ASSERT(JSValueIsInstanceOfConstructor(context, result, arrayConstructor, NULL)); ASSERT(!JSValueIsInstanceOfConstructor(context, JSValueMakeNull(context), arrayConstructor, NULL)); o = JSValueToObject(context, result, NULL); exception = NULL; ASSERT(JSValueIsUndefined(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception))); ASSERT(!exception); JSObjectSetPropertyAtIndex(context, o, 0, JSValueMakeNumber(context, 1), &exception); ASSERT(!exception); exception = NULL; ASSERT(1 == JSValueToNumber(context, JSObjectGetPropertyAtIndex(context, o, 0, &exception), &exception)); ASSERT(!exception); JSStringRef functionBody; JSObjectRef function; exception = NULL; functionBody = JSStringCreateWithUTF8CString("rreturn Array;"); JSStringRef line = JSStringCreateWithUTF8CString("line"); ASSERT(!JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception)); ASSERT(JSValueIsObject(context, exception)); v = JSObjectGetProperty(context, JSValueToObject(context, exception, NULL), line, NULL); assertEqualsAsNumber(v, 1); JSStringRelease(functionBody); JSStringRelease(line); exception = NULL; functionBody = JSStringCreateWithUTF8CString("return Array;"); function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, &exception); JSStringRelease(functionBody); ASSERT(!exception); ASSERT(JSObjectIsFunction(context, function)); v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); ASSERT(v); ASSERT(JSValueIsEqual(context, v, arrayConstructor, NULL)); exception = NULL; function = JSObjectMakeFunction(context, NULL, 0, NULL, jsEmptyIString, NULL, 0, &exception); ASSERT(!exception); v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, &exception); ASSERT(v && !exception); ASSERT(JSValueIsUndefined(context, v)); exception = NULL; v = NULL; JSStringRef foo = JSStringCreateWithUTF8CString("foo"); JSStringRef argumentNames[] = { foo }; functionBody = JSStringCreateWithUTF8CString("return foo;"); function = JSObjectMakeFunction(context, foo, 1, argumentNames, functionBody, NULL, 1, &exception); ASSERT(function && !exception); JSValueRef arguments[] = { JSValueMakeNumber(context, 2) }; v = JSObjectCallAsFunction(context, function, NULL, 1, arguments, &exception); JSStringRelease(foo); JSStringRelease(functionBody); string = JSValueToStringCopy(context, function, NULL); assertEqualsAsUTF8String(JSValueMakeString(context, string), "function foo(foo) {return foo;}"); JSStringRelease(string); JSStringRef print = JSStringCreateWithUTF8CString("print"); JSObjectRef printFunction = JSObjectMakeFunctionWithCallback(context, print, print_callAsFunction); JSObjectSetProperty(context, globalObject, print, printFunction, kJSPropertyAttributeNone, NULL); JSStringRelease(print); ASSERT(!JSObjectSetPrivate(printFunction, (void*)1)); ASSERT(!JSObjectGetPrivate(printFunction)); JSStringRef myConstructorIString = JSStringCreateWithUTF8CString("MyConstructor"); JSObjectRef myConstructor = JSObjectMakeConstructor(context, NULL, myConstructor_callAsConstructor); JSObjectSetProperty(context, globalObject, myConstructorIString, myConstructor, kJSPropertyAttributeNone, NULL); JSStringRelease(myConstructorIString); ASSERT(!JSObjectSetPrivate(myConstructor, (void*)1)); ASSERT(!JSObjectGetPrivate(myConstructor)); string = JSStringCreateWithUTF8CString("Derived"); JSObjectRef derivedConstructor = JSObjectMakeConstructor(context, Derived_class(context), NULL); JSObjectSetProperty(context, globalObject, string, derivedConstructor, kJSPropertyAttributeNone, NULL); JSStringRelease(string); o = JSObjectMake(context, NULL, NULL); JSObjectSetProperty(context, o, jsOneIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeNone, NULL); JSObjectSetProperty(context, o, jsCFIString, JSValueMakeNumber(context, 1), kJSPropertyAttributeDontEnum, NULL); JSPropertyNameArrayRef nameArray = JSObjectCopyPropertyNames(context, o); size_t expectedCount = JSPropertyNameArrayGetCount(nameArray); size_t count; for (count = 0; count < expectedCount; ++count) JSPropertyNameArrayGetNameAtIndex(nameArray, count); JSPropertyNameArrayRelease(nameArray); ASSERT(count == 1); // jsCFString should not be enumerated JSClassDefinition nullDefinition = kJSClassDefinitionEmpty; nullDefinition.attributes = kJSClassAttributeNoAutomaticPrototype; JSClassRef nullClass = JSClassCreate(&nullDefinition); JSClassRelease(nullClass); nullDefinition = kJSClassDefinitionEmpty; nullClass = JSClassCreate(&nullDefinition); JSClassRelease(nullClass); functionBody = JSStringCreateWithUTF8CString("return this;"); function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL); JSStringRelease(functionBody); v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL); ASSERT(JSValueIsEqual(context, v, o, NULL)); functionBody = JSStringCreateWithUTF8CString("return eval(\"this\");"); function = JSObjectMakeFunction(context, NULL, 0, NULL, functionBody, NULL, 1, NULL); JSStringRelease(functionBody); v = JSObjectCallAsFunction(context, function, NULL, 0, NULL, NULL); ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); v = JSObjectCallAsFunction(context, function, o, 0, NULL, NULL); ASSERT(JSValueIsEqual(context, v, o, NULL)); JSStringRef script = JSStringCreateWithUTF8CString("this;"); v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL); ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); v = JSEvaluateScript(context, script, o, NULL, 1, NULL); ASSERT(JSValueIsEqual(context, v, o, NULL)); JSStringRelease(script); script = JSStringCreateWithUTF8CString("eval(this);"); v = JSEvaluateScript(context, script, NULL, NULL, 1, NULL); ASSERT(JSValueIsEqual(context, v, globalObject, NULL)); v = JSEvaluateScript(context, script, o, NULL, 1, NULL); ASSERT(JSValueIsEqual(context, v, o, NULL)); JSStringRelease(script); char* scriptUTF8 = createStringWithContentsOfFile(scriptPath); if (!scriptUTF8) printf("FAIL: Test script could not be loaded.\n"); else { script = JSStringCreateWithUTF8CString(scriptUTF8); result = JSEvaluateScript(context, script, NULL, NULL, 1, &exception); if (JSValueIsUndefined(context, result)) printf("PASS: Test script executed successfully.\n"); else { printf("FAIL: Test script returned unexpected value:\n"); JSStringRef exceptionIString = JSValueToStringCopy(context, exception, NULL); CFStringRef exceptionCF = JSStringCopyCFString(kCFAllocatorDefault, exceptionIString); CFShow(exceptionCF); CFRelease(exceptionCF); JSStringRelease(exceptionIString); } JSStringRelease(script); free(scriptUTF8); } // Clear out local variables pointing at JSObjectRefs to allow their values to be collected function = NULL; v = NULL; o = NULL; globalObject = NULL; JSStringRelease(jsEmptyIString); JSStringRelease(jsOneIString); JSStringRelease(jsCFIString); JSStringRelease(jsCFEmptyIString); JSStringRelease(jsCFIStringWithCharacters); JSStringRelease(jsCFEmptyIStringWithCharacters); JSStringRelease(goodSyntax); JSStringRelease(badSyntax); JSGlobalContextRelease(context); JSClassRelease(globalObjectClass); printf("PASS: Program exited normally.\n"); return 0; }