/**
 * internal 
 *
 * setup a context after created
 */
static void InitializeContext (JSGlobalContextRef ctx)
{
    auto global = JSContextGetGlobalObject(ctx);
    auto setterProps = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete;

    // inject a simple console logger
    auto logProperty = JSStringCreateWithUTF8CString("log");
    auto consoleProperty = JSStringCreateWithUTF8CString("console");
    auto consoleObject = JSObjectMake(ctx, 0, 0);
    auto logFunction = JSObjectMakeFunctionWithCallback(ctx, logProperty, HyperloopLogger);
    JSObjectSetProperty(ctx, consoleObject, logProperty, logFunction, setterProps, 0);
    JSObjectSetProperty(ctx, global, consoleProperty, consoleObject, setterProps, 0);
    JSStringRelease(logProperty);
    JSStringRelease(consoleProperty);

    // bind some internal cross-platform methods
    auto vmBindingProperty = JSStringCreateWithUTF8CString("hyperloop$vm");
    auto vmrunInNewContextProperty = JSStringCreateWithUTF8CString("runInNewContext");
    auto vmBindingObject = JSObjectMake(ctx, 0, 0);
    auto vmrunInNewContextFunction = JSObjectMakeFunctionWithCallback(ctx, vmrunInNewContextProperty, RunInNewContext);
    JSObjectSetProperty(ctx, vmBindingObject, vmrunInNewContextProperty, vmrunInNewContextFunction, setterProps, 0);
    JSObjectSetProperty(ctx, global, vmBindingProperty, vmBindingObject, setterProps, 0);
    JSStringRelease(vmBindingProperty);
    JSStringRelease(vmrunInNewContextProperty);

    // create a hook into our global context
    auto prop = JSStringCreateWithUTF8CString("hyperloop$global");
    JSObjectSetProperty(ctx, global, prop, global, setterProps, 0);
    JSStringRelease(prop);

    // setup our globals object -- should point to the real root global object if a new context (not the root ctx)
    auto globalProperty = JSStringCreateWithUTF8CString("global");
    JSObjectSetProperty(ctx, global, globalProperty, global, setterProps, 0);
    JSStringRelease(globalProperty);
}
Beispiel #2
0
void webWorkerInitialize(JSContextRef context, int isDedicated)
{
    if (isDedicated) {
        JSStringRef funcName = JSStringCreateWithUTF8CString("foo");
        JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(context, funcName, &foo);
        JSObjectSetProperty(context, JSContextGetGlobalObject(context), funcName, jsFunc, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
        JSStringRelease(funcName);
    } else {
        JSStringRef funcName = JSStringCreateWithUTF8CString("bar");
        JSObjectRef jsFunc = JSObjectMakeFunctionWithCallback(context, funcName, &bar);
        JSObjectSetProperty(context, JSContextGetGlobalObject(context), funcName, jsFunc, kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontDelete, 0);
        JSStringRelease(funcName);
    }
}
Beispiel #3
0
	void JSClass::ConstructorInitializeCallback(const JSContext& js_context, JSObject& this_object) const {
		if (!this_object.HasProperty("prototype")) {
			auto js_object = js_context.CreateObject();
			this_object.SetProperty("prototype", js_object);
		}

		auto proto_object = static_cast<JSObject>(this_object.GetProperty("prototype"));
		for (const auto v : prototype_functions_map__) {
			//
			// NOTE: On HAL, there's no difference between object "static" property and prototype property
			// so add it to both
			//
			this_object.SetProperty(v.first, JSObjectMakeFunctionWithCallback(js_context, v.first, v.second));
			proto_object.SetProperty(v.first, JSObjectMakeFunctionWithCallback(js_context, v.first, v.second));
		}
	}
Beispiel #4
0
JSObjectRef _JS_fn(JSContextRef ctx, const char *name, JSObjectCallAsFunctionCallback f)
{
    JSStringRef nameStr = JSStringCreateWithUTF8CString(name);
    JSObjectRef value = JSObjectMakeFunctionWithCallback(ctx, nameStr, f);
    JSStringRelease(nameStr);
    return value;
}
static void window_object_cleared_callback(WebKitWebView* web_view, WebKitWebFrame* web_frame, JSGlobalContextRef context, JSObjectRef window_object, gpointer data)
{
    JSStringRef name = JSStringCreateWithUTF8CString("runTest");
    JSObjectRef testComplete = JSObjectMakeFunctionWithCallback(context, name, runPasteTestCallback);
    JSObjectSetProperty(context, window_object, name, testComplete, kJSPropertyAttributeNone, 0);
    JSStringRelease(name);
}
Beispiel #6
0
//
//  Boilerplate code / signal callback for attaching methods when a
//  new javascript context is created.
//
static void 
window_object_cleared_callback (WebKitScriptWorld *world, 
                                WebKitWebPage *web_page, 
                                WebKitFrame *frame, 
                                gpointer user_data)
{
  std::cout << "attempting to set up custom c function bindings\n";
  JSGlobalContextRef js_ctx;
  js_ctx = webkit_frame_get_javascript_context_for_script_world (frame, world);
  
  JSStringRef function_name = JSStringCreateWithUTF8CString("whatever");
  JSObjectRef boiler_plate = JSObjectMakeFunctionWithCallback(js_ctx,
                                                              function_name,
                                                              some_method);
  JSValueRef exception = 0;
  JSObjectRef global = JSContextGetGlobalObject(js_ctx);
  JSObjectSetProperty(js_ctx,
                      global,
                      JSStringCreateWithUTF8CString("myCFunction"),
                      boiler_plate,
                      kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly,
                      &exception);
  if (exception) {
    std::cout << "Argh! an exception!!!!\n";
  }
}
Beispiel #7
0
JSObjectRef makeFunction(
    JSGlobalContextRef ctx,
    const char* name,
    JSObjectCallAsFunctionCallback callback) {
  auto jsName = String(name);
  return JSObjectMakeFunctionWithCallback(ctx, jsName, callback);
}
JSValueRef MDNativeBindingFunction::propertyValue(JSContextRef context)
{
    JSStringRef strRef = JSStringCreateWithUTF8CString(m_jsNativeFunc.name);
    JSValueRef valueRef = JSObjectMakeFunctionWithCallback(context, strRef, m_jsNativeFunc.callAsFunction);
    JSStringRelease(strRef);
    return valueRef;

}
Beispiel #9
0
void register_global_function(JSContextRef ctx, char *name, JSObjectCallAsFunctionCallback handler) {
	JSObjectRef global_obj = JSContextGetGlobalObject(ctx);

	JSStringRef fn_name = JSStringCreateWithUTF8CString(name);
	JSObjectRef fn_obj = JSObjectMakeFunctionWithCallback(ctx, fn_name, handler);

	JSObjectSetProperty(ctx, global_obj, fn_name, fn_obj, kJSPropertyAttributeNone, NULL);
}
JNIEXPORT jint JNICALL WebKit_win32_NATIVE(JSObjectMakeFunctionWithCallback)
	(JNIEnv *env, jclass that, jintLong arg0, jintLong arg1, jintLong arg2)
{
	jint rc = 0;
	WebKit_win32_NATIVE_ENTER(env, that, JSObjectMakeFunctionWithCallback_FUNC);
	rc = (jint)JSObjectMakeFunctionWithCallback((JSContextRef)arg0, (JSStringRef)arg1, (JSObjectCallAsFunctionCallback)arg2);
	WebKit_win32_NATIVE_EXIT(env, that, JSObjectMakeFunctionWithCallback_FUNC);
	return rc;
}
Beispiel #11
0
void installGlobalFunction(
    JSGlobalContextRef ctx,
    const char* name,
    JSObjectCallAsFunctionCallback callback) {
  String jsName(ctx, name);
  JSObjectRef functionObj = JSObjectMakeFunctionWithCallback(
    ctx, jsName, callback);
  Object::getGlobalObject(ctx).setProperty(jsName, Value(ctx, functionObj));
}
Beispiel #12
0
int
main (int    argc,
      gchar* argv[])
{
  GtkWidget* window;
  GtkWidget* vbox;
  GtkWidget* scrolled;
  GtkWidget* web_view;
  GtkWidget* button_box;
  WebKitWebFrame* web_frame;
  JSGlobalContextRef js_context;
  JSObjectRef js_global;
  JSStringRef js_function_name;
  JSObjectRef js_set_can_register;

  if (!g_thread_supported ())
    g_thread_init (NULL);
  gtk_init_check (&argc, &argv);

  window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  gtk_window_set_title (GTK_WINDOW (window), "WebKitGTK+ API Demo");
  gtk_window_set_default_size (GTK_WINDOW (window), 640, 240);
  g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
  vbox = gtk_vbox_new (FALSE, 4);
  gtk_container_add (GTK_CONTAINER (window), vbox);

  scrolled = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
                                  GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 0);
  web_view = webkit_web_view_new ();
  gtk_container_add (GTK_CONTAINER (scrolled), web_view);
  webkit_web_view_load_string (WEBKIT_WEB_VIEW (web_view), form_markup, "text/html", "UTF-8", "");
  g_signal_connect (web_view, "create-plugin-widget",
                    G_CALLBACK (web_view_create_plugin_widget_cb), NULL);

  button_box = gtk_hbutton_box_new ();
  gtk_box_pack_start (GTK_BOX (vbox), button_box, FALSE, FALSE, 0);
  register_button = gtk_button_new_with_mnemonic ("Send _Registration");
  gtk_widget_set_sensitive (register_button, FALSE);
  gtk_box_pack_start (GTK_BOX (button_box), register_button, FALSE, FALSE, 0);

  web_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (web_view));
  js_context = webkit_web_frame_get_global_context (web_frame);
  js_global = JSContextGetGlobalObject (js_context);
  js_function_name = JSStringCreateWithUTF8CString ("setCanRegister");
  js_set_can_register = JSObjectMakeFunctionWithCallback (js_context,
    js_function_name, (JSObjectCallAsFunctionCallback)set_can_register_cb);
  JSObjectSetProperty (js_context, js_global, js_function_name, js_set_can_register, 0, NULL);
  JSStringRelease (js_function_name);

  gtk_widget_show_all (window);

  gtk_main ();

  return 0;
}
Beispiel #13
0
void installGlobalFunction(
    JSGlobalContextRef ctx,
    const char* name,
    JSObjectCallAsFunctionCallback callback) {
  JSStringRef jsName = JSStringCreateWithUTF8CString(name);
  JSObjectRef functionObj = JSObjectMakeFunctionWithCallback(
      ctx, jsName, callback);
  JSObjectRef globalObject = JSContextGetGlobalObject(ctx);
  JSObjectSetProperty(ctx, globalObject, jsName, functionObj, 0, NULL);
  JSStringRelease(jsName);
}
static void windowObjectCleared(WebKitScriptWorld* world, WebKitWebPage* page, WebKitFrame* frame, gpointer)
{
    JSGlobalContextRef jsContext = webkit_frame_get_javascript_context_for_script_world(frame, world);
    g_assert(jsContext);
    JSObjectRef globalObject = JSContextGetGlobalObject(jsContext);
    g_assert(globalObject);

    JSRetainPtr<JSStringRef> functionName(Adopt, JSStringCreateWithUTF8CString("echo"));
    JSObjectRef function = JSObjectMakeFunctionWithCallback(jsContext, functionName.get(), echoCallback);
    JSObjectSetProperty(jsContext, globalObject, functionName.get(), function, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
}
Beispiel #15
0
void VJSObject::MakeCallback( JS4D::ObjectCallAsFunctionCallback inCallbackFunction)
{
	JS4D::ContextRef	contextRef;
	
	if ((contextRef = fContext) != NULL && inCallbackFunction != NULL) 

		// Use NULL for function name (anonymous).

		fObject = JSObjectMakeFunctionWithCallback( contextRef, NULL, inCallbackFunction);

	else

		SetNull();
}
void mk_zs__(WebKitWebView* wwv){
	WebKitWebFrame* web_frame;
	JSGlobalContextRef js_context;
	JSObjectRef js_global;
	JSStringRef js_function_name;
	JSObjectRef js_function;
	web_frame = webkit_web_view_get_main_frame (WEBKIT_WEB_VIEW (wwv));
	js_context = webkit_web_frame_get_global_context (web_frame);
	js_global = JSContextGetGlobalObject (js_context);
	js_function_name = JSStringCreateWithUTF8CString (s1_[zs_].c_str());
	js_function = JSObjectMakeFunctionWithCallback (js_context,
			js_function_name, zs__);
	JSObjectSetProperty (js_context, js_global, js_function_name, js_function,
			kJSPropertyAttributeNone, NULL);
	JSStringRelease (js_function_name);
}
static JSValueRef getProperty(JSContextRef jscore_ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef *exception)
{
	pdf_jsimp *imp;
	char buf[STRING_BUF_SIZE];
	prop *p;
	JSValueRef res = NULL;

	priv_data *pdata = JSObjectGetPrivate(object);
	if (pdata == NULL)
		return NULL;

	JSStringGetUTF8CString(propertyName, buf, STRING_BUF_SIZE);
	p = find_prop(pdata->type->props, buf);
	if (p == NULL)
		return NULL;

	imp = pdata->type->imp;

	switch(p->type)
	{
		case PROP_FN:
			{
				/*
					For some reason passing the method pointer as private data doesn't work: the data comes back
					NULL when interrogated in callMethod above. So we also specify the method name when
					creating the function so that we can look it up again in callMethod. Not ideal, but
					will do until we can find a better solution.
				*/
				JSObjectRef ores = JSObjectMakeFunctionWithCallback(jscore_ctx, propertyName, callMethod);
				JSObjectSetPrivate(ores, p->u.fn.meth);
				res = ores;
			}
			break;

		case PROP_VAL:
			{
				pdf_jsimp_obj *pres = p->u.val.get(imp->nat_ctx, pdata->natobj);
				res = pres->ref;
				pdf_jsimp_drop_obj(imp, pres);
			}
			break;
	}

	return res;
}
Beispiel #18
0
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;
}
Beispiel #19
0
void defineGlobals (JSContextRef context, entry globals[]) {
  JSObjectRef g = JSContextGetGlobalObject(context);
  int i = 0;
  while (1) {
    entry global = globals[i++];
    if (global.name == NULL)
      break;
    JSStringRef name = JSStringCreateWithUTF8CString(global.name);
    JSValueRef value;
    if (global.type == FUNCTION)
      value = JSObjectMakeFunctionWithCallback(context, name,
                                               global.value.function);
    else
      value = global.value.value;

    JSObjectSetProperty(context, g, name, value,
                        kJSPropertyAttributeNone, NULL);
    JSStringRelease(name);
  }
}
Beispiel #20
0
void QtBuiltinBundlePage::registerNavigatorQtObject(JSGlobalContextRef context)
{
    static JSStringRef postMessageName = JSStringCreateWithUTF8CString("postMessage");
    static JSStringRef navigatorName = JSStringCreateWithUTF8CString("navigator");
    static JSStringRef qtName = JSStringCreateWithUTF8CString("qt");

    if (m_navigatorQtObject)
        JSValueUnprotect(context, m_navigatorQtObject);
    m_navigatorQtObject = JSObjectMake(context, navigatorQtObjectClass(), this);
    JSValueProtect(context, m_navigatorQtObject);

    JSObjectRef postMessage = JSObjectMakeFunctionWithCallback(context, postMessageName, qt_postMessageCallback);
    JSObjectSetProperty(context, m_navigatorQtObject, postMessageName, postMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);

    JSValueRef navigatorValue = JSObjectGetProperty(context, JSContextGetGlobalObject(context), navigatorName, 0);
    if (!JSValueIsObject(context, navigatorValue))
        return;
    JSObjectRef navigatorObject = JSValueToObject(context, navigatorValue, 0);
    JSObjectSetProperty(context, navigatorObject, qtName, m_navigatorQtObject, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
}
static void registerNavigatorObject(JSObjectRef *object, JSStringRef name,
                                    JSGlobalContextRef context, void* data,
                                    CreateClassRefCallback createClassRefCallback,
                                    JSStringRef postMessageName, JSObjectCallAsFunctionCallback postMessageCallback)
{
    static JSStringRef navigatorName = JSStringCreateWithUTF8CString("navigator");

    if (*object)
        JSValueUnprotect(context, *object);
    *object = JSObjectMake(context, createClassRefCallback(), data);
    JSValueProtect(context, *object);

    JSObjectRef postMessage = JSObjectMakeFunctionWithCallback(context, postMessageName, postMessageCallback);
    JSObjectSetProperty(context, *object, postMessageName, postMessage, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);

    JSValueRef navigatorValue = JSObjectGetProperty(context, JSContextGetGlobalObject(context), navigatorName, 0);
    if (!JSValueIsObject(context, navigatorValue))
        return;
    JSObjectRef navigatorObject = JSValueToObject(context, navigatorValue, 0);
    JSObjectSetProperty(context, navigatorObject, name, *object, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);
}
Beispiel #22
0
void PagePopupBlackBerry::installDOMFunction(Frame* frame)
{
    JSDOMWindow* window = toJSDOMWindow(frame, mainThreadNormalWorld());
    ASSERT(window);

    JSC::ExecState* exec = window->globalExec();
    ASSERT(exec);
    JSC::JSLockHolder lock(exec);

    JSContextRef context = ::toRef(exec);
    JSObjectRef globalObject = JSContextGetGlobalObject(context);
    JSStringRef functionName = JSStringCreateWithUTF8CString(
            "setValueAndClosePopup");
    JSObjectRef function = JSObjectMakeFunctionWithCallback(context,
            functionName, setValueAndClosePopupCallback);
    JSObjectSetProperty(context, globalObject, functionName, function,
            kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly, 0);

    // Register client into DOM
    JSClassDefinition definition = kJSClassDefinitionEmpty;
    definition.staticValues = popUpExtensionStaticValues;
    definition.staticFunctions = popUpExtensionStaticFunctions;
    definition.initialize = popUpExtensionInitialize;
    definition.finalize = popUpExtensionFinalize;
    JSClassRef clientClass = JSClassCreate(&definition);

    JSObjectRef clientClassObject = JSObjectMake(context, clientClass, 0);

    // Add a reference. See popUpExtensionFinalize.
    m_sharedClientPointer->ref();
    JSObjectSetPrivate(clientClassObject, m_sharedClientPointer.get());

    String name("popUp");

    JSC::PutPropertySlot slot;
    window->put(window, exec, JSC::Identifier(exec, name),
            toJS(clientClassObject), slot);

    JSClassRelease(clientClass);
}
Beispiel #23
0
JSObjectRef RJSConstructorCreate(JSContextRef ctx) {
    static JSStringRef clearTestStateString = JSStringCreateWithUTF8CString("clearTestState");
    static JSStringRef listString = JSStringCreateWithUTF8CString("List");
    static JSStringRef resultsString = JSStringCreateWithUTF8CString("Results");
    static JSStringRef typeString = JSStringCreateWithUTF8CString("Types");

    JSObjectRef realmObject = JSObjectMake(ctx, RJSRealmConstructorClass(), NULL);
    JSPropertyAttributes attributes = kJSPropertyAttributeReadOnly | kJSPropertyAttributeDontEnum | kJSPropertyAttributeDontDelete;

    JSObjectRef listConstructor = JSObjectMakeConstructor(ctx, RJSListClass(), UncallableConstructor);
    RJSValidatedSetProperty(ctx, realmObject, listString, listConstructor, attributes);

    JSObjectRef resultsContructor = JSObjectMakeConstructor(ctx, RJSResultsClass(), UncallableConstructor);
    RJSValidatedSetProperty(ctx, realmObject, resultsString, resultsContructor, attributes);

    JSObjectRef typesObject = JSObjectMake(ctx, RJSRealmTypeClass(), NULL);
    RJSValidatedSetProperty(ctx, realmObject, typeString, typesObject, attributes);

    JSObjectRef clearTestStateFunction = JSObjectMakeFunctionWithCallback(ctx, clearTestStateString, ClearTestState);
    RJSValidatedSetProperty(ctx, realmObject, clearTestStateString, clearTestStateFunction, attributes);

    return realmObject;
}
Beispiel #24
0
// get property for each methods, new methods should be added here
JSValueRef spjsdpplugin_get_property_cb(JSContextRef ctx, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
{
    bool ret = false;
    JSValueRef retval;
    int propertySize = JSStringGetMaximumUTF8CStringSize(propertyName);
    char* property = (char*)malloc(propertySize*sizeof(char));
    JSStringGetUTF8CString(propertyName, property, propertySize);
    if(strcmp(kMethodActivateOnline,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodActivateOnline);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_activate_online);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodValidatePassword,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodValidatePassword);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_validate_password);
        JSStringRelease(str);
        ret = true;
    }
    
    else if(strcmp(kMethodGenerateOTP,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodGenerateOTP);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_generate_otp);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodGenerateDerivationCode,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodGenerateDerivationCode);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_generate_derivation_code);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodChangePassword,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodChangePassword);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_change_password);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodGetInfo, property) == 0){
        JSStringRef str = JSStringCreateWithUTF8CString(kMethodGetInfo);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_get_info);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodComputeTimeShift,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodComputeTimeShift);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_compute_timeshift);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodInitializeRegistrationDataV2,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodInitializeRegistrationDataV2);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_initialize_registration_dataV2);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodDecryptActivationData,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodDecryptActivationData);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_decrypt_activation_data);
        JSStringRelease(str);
        ret = true;
    }
    else if(strcmp(kMethodValidateSharedDataChecksum,property) == 0)
    {
		JSStringRef str = JSStringCreateWithUTF8CString(kMethodValidateSharedDataChecksum);
        retval = (JSValueRef)JSObjectMakeFunctionWithCallback(ctx, str, js_validate_shared_data_checksum);
        JSStringRelease(str);
        ret = true;
    }
    free(property);
    return retval;
}
Beispiel #25
0
gint
main (gint argc, gchar ** argv)
{
  JSGlobalContextRef ctx;

  JSStringRef str;
  JSObjectRef func;
  JSValueRef result;
  JSValueRef exception = NULL;

  JsonObject *obj;
  JsonNode *node;

  g_type_init();

  gchar *buffer;

  if (argc != 2)
    {
      printf ("Usage: %s <script>\n", argv[0]);
      return 1;
    }

  ctx = JSGlobalContextCreate (NULL);

  str = JSStringCreateWithUTF8CString ("emitIntermediate");
  func = JSObjectMakeFunctionWithCallback (ctx, str, js_emitIntermediate);
  JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func,
		       kJSPropertyAttributeNone, NULL);
  JSStringRelease (str);

  str = JSStringCreateWithUTF8CString ("emit");
  func = JSObjectMakeFunctionWithCallback (ctx, str, js_emit);
  JSObjectSetProperty (ctx, JSContextGetGlobalObject (ctx), str, func,
		       kJSPropertyAttributeNone, NULL);
  JSStringRelease (str);

  str = JSStringCreateWithUTF8CString (argv[1]);
  result = JSEvaluateScript (ctx, str, NULL, NULL, 0, &exception);
  JSStringRelease (str);

  obj = json_object_new ();

  if (!result || exception)
    {
      js_value (ctx, exception, &node);

      json_object_set_member (obj, "error", node);

      json_object_set_boolean_member (obj, "status", FALSE);

      JSGlobalContextRelease (ctx);
    }
  else
    {
      json_object_set_boolean_member (obj, "status", TRUE);
    }

  JsonNode *node1 = json_node_new (JSON_NODE_OBJECT);

  if (node1 == NULL)
    {
      json_object_unref (obj);
      JSGlobalContextRelease (ctx);
      return 1;
    }

  json_node_set_object (node1, obj);

  JsonGenerator *gen = json_generator_new();

  if (gen == NULL)
    {
      json_node_free (node1);
      JSGlobalContextRelease (ctx);
      return 1;
    }

  json_generator_set_root (gen, node1 );
  buffer = json_generator_to_data (gen,NULL);

  if (buffer == NULL)
    {
      json_node_free (node1);
      JSGlobalContextRelease (ctx);
      return 1;
    }

  json_node_free (node1);

  puts (buffer);
  g_free (buffer);

  JSGlobalContextRelease (ctx);
  return 0;
}
Beispiel #26
0
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;
}