static string toStr(WKBundlePageRef page, WKBundleScriptWorldRef world, WKBundleRangeHandleRef rangeRef) { if (!rangeRef) return "(null)"; WKBundleFrameRef frame = WKBundlePageGetMainFrame(page); JSGlobalContextRef context = WKBundleFrameGetJavaScriptContextForWorld(frame, world); JSValueRef rangeValue = WKBundleFrameGetJavaScriptWrapperForRangeForWorld(frame, rangeRef, world); ASSERT(JSValueIsObject(context, rangeValue)); JSObjectRef rangeObject = (JSObjectRef)rangeValue; JSValueRef startNodeValue = propertyValue(context, rangeObject, "startContainer"); ASSERT(JSValueIsObject(context, startNodeValue)); JSObjectRef startNodeObject = (JSObjectRef)startNodeValue; JSValueRef endNodeValue = propertyValue(context, rangeObject, "endContainer"); ASSERT(JSValueIsObject(context, endNodeValue)); JSObjectRef endNodeObject = (JSObjectRef)endNodeValue; int startOffset = propertyValueInt(context, rangeObject, "startOffset"); int endOffset = propertyValueInt(context, rangeObject, "endOffset"); ostringstream out; out << "range from " << startOffset << " of " << dumpPath(context, startNodeObject) << " to " << endOffset << " of " << dumpPath(context, endNodeObject); return out.str(); }
EXPORTAPI JSValueRef Hyperloop_Binary_IsEqual(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { if (argumentCount < 2) { *exception = HyperloopMakeException(ctx, "Wrong arguments passed to IsEqual"); return JSValueMakeUndefined(ctx); } auto isObject_a = JSValueIsObject(ctx, arguments[0]); auto isObject_b = JSValueIsObject(ctx, arguments[1]); if (isObject_a && isObject_b) { auto obj_a = HyperloopJSValueToVoidPointer(ctx, arguments[0], exception); auto obj_b = HyperloopJSValueToVoidPointer(ctx, arguments[1], exception); // nullptr means both objects are not a native object if (obj_a == nullptr && obj_b == nullptr) { return JSValueMakeBoolean(ctx, JSValueIsStrictEqual(ctx, arguments[0], arguments[1])); } return JSValueMakeBoolean(ctx, (obj_a == obj_b)); } else if (!isObject_a && !isObject_b) { return JSValueMakeBoolean(ctx, JSValueIsStrictEqual(ctx, arguments[0], arguments[1])); } return JSValueMakeBoolean(ctx, false); }
static JSObjectRef propertyObject(JSContextRef context, JSObjectRef object, const char* propertyName) { JSValueRef value = propertyValue(context, object, propertyName); if (!value || !JSValueIsObject(context, value)) return 0; return const_cast<JSObjectRef>(value); }
Type GetType(JSValueRef value) { if (JSValueIsNull(g_ctx, value)) return VTYPE_NULL; if (JSValueIsBoolean(g_ctx, value)) return VTYPE_BOOL; if (JSValueIsNumber(g_ctx, value)) return VTYPE_DOUBLE; if (JSValueIsString(g_ctx, value)) return VTYPE_STRING; if (JSValueIsObject(g_ctx, value)) { JSObjectRef obj = JSValueToObject(g_ctx, value, NULL); if (JSObjectIsFunction(g_ctx, obj)) return VTYPE_FUNCTION; if (IsArray(obj)) return VTYPE_LIST; return VTYPE_DICTIONARY; } return VTYPE_INVALID; }
static JSObjectRef getElementById(WKBundleFrameRef frame, JSStringRef elementId) { JSContextRef context = WKBundleFrameGetJavaScriptContext(frame); JSObjectRef document = propertyObject(context, JSContextGetGlobalObject(context), "document"); if (!document) return 0; JSValueRef getElementById = propertyObject(context, document, "getElementById"); if (!getElementById || !JSValueIsObject(context, getElementById)) return 0; JSValueRef elementIdValue = JSValueMakeString(context, elementId); JSValueRef exception; JSValueRef element = JSObjectCallAsFunction(context, const_cast<JSObjectRef>(getElementById), document, 1, &elementIdValue, &exception); if (!element || !JSValueIsObject(context, element)) return 0; return const_cast<JSObjectRef>(element); }
/* * Tests whether a JavaScript value is an array object * * This invokes Array.isArray(value) and returns its result */ EXPORTAPI bool HyperloopJSValueIsArray(JSContextRef ctx, JSValueRef value) { if (JSValueIsObject(ctx, value)) { JSObjectRef global = JSContextGetGlobalObject(ctx); JSValueRef exception = JSValueMakeNull(ctx); JSStringRef string = JSStringCreateWithUTF8CString("Array"); JSObjectRef array = JSValueToObject(ctx, JSObjectGetProperty(ctx, global, string, &exception), &exception); JSStringRelease(string); if (!JSValueIsNull(ctx, exception)) { return false; } string = JSStringCreateWithUTF8CString("isArray"); JSObjectRef isArray = JSValueToObject(ctx, JSObjectGetProperty(ctx, array, string, &exception), &exception); JSStringRelease(string); if (!JSValueIsNull(ctx, exception)) { return false; } JSValueRef result = JSObjectCallAsFunction(ctx, isArray, global, 1, &value, &exception); if (JSValueIsNull(ctx, exception) && JSValueIsBoolean(ctx, result)) { return JSValueToBoolean(ctx, result); } } return false; }
static JSValueRef js_emit (JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { JsonObject *obj; JsonNode *node; gchar *buffer; if (argumentCount != 1) { *exception = JSValueMakeNumber (ctx, 1); return NULL; } if (JSValueIsObject (ctx, arguments[0]) == false) { *exception = JSValueMakeNumber (ctx, 1); return NULL; } obj = json_object_new(); js_value (ctx, (JSObjectRef) arguments[0], &node); json_object_set_member (obj, "emit", node); JsonNode *node1 = json_node_new (JSON_NODE_OBJECT); if (node1 == NULL) { json_object_unref (obj); return NULL; } json_node_set_object (node1, obj); JsonGenerator *gen = json_generator_new(); if (gen == NULL) { json_node_free (node1); return NULL; } json_generator_set_root (gen, node1 ); buffer = json_generator_to_data (gen,NULL); if (buffer == NULL) { json_node_free (node1); g_object_unref (gen); return NULL; } json_node_free (node1); g_object_unref (gen); puts (buffer); g_free (buffer); return NULL; /* shouldn't be an object ? */ }
VJSObject VJSObject::GetPropertyAsObject( const VString& inPropertyName, JS4D::ExceptionRef *outException) const { JSStringRef jsName = JS4D::VStringToString( inPropertyName); JSValueRef value = JSObjectGetProperty( fContext, fObject, jsName, outException); JSObjectRef objectRef = ((value != NULL) && JSValueIsObject( fContext, value)) ? JSValueToObject( fContext, value, outException) : NULL; JSStringRelease( jsName); return VJSObject( fContext, objectRef); }
void LayoutTestController::setValueForUser(JSContextRef context, JSValueRef element, JSStringRef value) { if (!element || !JSValueIsObject(context, element)) return; WKRetainPtr<WKBundleNodeHandleRef> nodeHandle(AdoptWK, WKBundleNodeHandleCreate(context, const_cast<JSObjectRef>(element))); WKBundleNodeHandleSetHTMLInputElementValueForUser(nodeHandle.get(), toWK(value).get()); }
static JSValueRef seed_gobject_signal_connect_on_property (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { gulong id = 0; JSObjectRef this_obj; signal_privates *privates; privates = (signal_privates *) JSObjectGetPrivate (thisObject); if (!privates) g_error ("Signal constructed with invalid parameters" "in namespace import \n"); this_obj = (JSObjectRef) seed_value_from_object (ctx, privates->object, exception); if ((argumentCount > 2) || (argumentCount == 0)) { seed_make_exception (ctx, exception, "ArgumentError", "Signal connection expected" " 1, or 2 arguments. Got " "%zd", argumentCount); return JSValueMakeNull (ctx); } if (JSValueIsNull (ctx, arguments[0]) || !JSValueIsObject (ctx, arguments[0]) || !JSObjectIsFunction (ctx, (JSObjectRef) arguments[0])) { seed_make_exception (ctx, exception, "ArgumentError", "Signal connection requires a function" " as first argument"); return JSValueMakeNull (ctx); } if (argumentCount == 1) { id = seed_gobject_signal_connect (ctx, privates->signal_name, privates->object, (JSObjectRef) arguments[0], this_obj, NULL); } else if (argumentCount == 2) { id = seed_gobject_signal_connect (ctx, privates->signal_name, privates->object, (JSObjectRef) arguments[0], this_obj, (JSObjectRef) arguments[1]); } return seed_value_from_ulong (ctx, id, exception); }
/** * seed_exception_get_line: * @ctx: A #SeedContext. * @exception: A reference to a #SeedException. * * Retrieves the line number the given exception was thrown from; keep in mind * that exceptions created from C have an undefined line number. * * Return value: A #guint representing the line number from which @exception * was thrown. * */ guint seed_exception_get_line (JSContextRef ctx, JSValueRef e) { JSValueRef line; g_assert ((e)); if (!JSValueIsObject (ctx, e)) return 0; line = seed_object_get_property (ctx, (JSObjectRef) e, "line"); return seed_value_to_uint (ctx, line, NULL); }
/** * seed_exception_get_file: * @ctx: A #SeedContext. * @exception: A reference to a #SeedException. * * Retrieves the file name the given exception was thrown from; keep in mind * that exceptions created from C have an undefined file name. * * Return value: A #gchar* representing the name of the file from which * @exception was thrown. * */ gchar * seed_exception_get_file (JSContextRef ctx, JSValueRef e) { JSValueRef line; g_assert ((e)); if (!JSValueIsObject (ctx, e)) return 0; line = seed_object_get_property (ctx, (JSObjectRef) e, "sourceURL"); return seed_value_to_string (ctx, line, 0); }
/** * invoke a function callback */ EXPORTAPI JSValueRef HyperloopInvokeFunctionCallback (JSContextRef ctx, void * callbackPointer, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { JSValueRef callback = *(JSValueRef*)callbackPointer; if (!JSValueIsObject(ctx, callback) || !JSObjectIsFunction(ctx, JSValueToObject(ctx,callback,exception))) { *exception = HyperloopMakeException(ctx,"Function callback is not a JS function object"); return JSValueMakeUndefined(ctx); } JSObjectRef callbackObj = JSValueToObject(ctx,callback,exception); return JSObjectCallAsFunction(HyperloopGlobalContext(), callbackObj, NULL, argumentCount, arguments, exception); }
VJSObject VJSValue::GetObject( JS4D::ExceptionRef *outException) const { VJSObject resultObj(fContext); if ((fValue != NULL) && JSValueIsObject( fContext, fValue)) resultObj.SetObjectRef( JSValueToObject( fContext, fValue, outException)); else //resultObj.SetObjectRef(JSValueToObject(fContext, JSValueMakeNull(fContext), outException)); resultObj.MakeEmpty(); return resultObj; }
void JSArray::push(const JSValue& val) { JSValueRef prop = JSObjectGetProperty(ctx_, instance_, JSString("push"), nullptr); if (JSValueIsObject(ctx_, prop)) { JSObjectRef func = JSValueToObject(ctx_, prop, nullptr); if (JSObjectIsFunction(ctx_, func)) { JSValueRef arg = val; JSObjectCallAsFunction(ctx_, func, instance_, 1, &arg, nullptr); } } }
JSRetainPtr<JSStringRef> LayoutTestController::markerTextForListItem(JSValueRef element) { WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page()); JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame); if (!element || !JSValueIsObject(context, element)) return 0; WKRetainPtr<WKStringRef> text(AdoptWK, WKBundleFrameCopyMarkerText(mainFrame, const_cast<JSObjectRef>(element))); if (WKStringIsEmpty(text.get())) return 0; return toJS(text); }
EJ_BIND_FUNCTION(EJBindingCanvas, drawImage, ctx, argc, argv) { if( argc < 3 || !JSValueIsObject(ctx, argv[0]) ) return NULL; EJBindingImage* drawable = (EJBindingImage*)JSObjectGetPrivate((JSObjectRef)argv[0]); // NSObject<EJDrawable> * drawable = (NSObject<EJDrawable> *)JSObjectGetPrivate((JSObjectRef)argv[0]); EJTexture * image = drawable->texture; float scale = image?image->contentScale:1; short sx = 0, sy = 0, sw = 0, sh = 0; float dx = 0, dy = 0, dw = sw, dh = sh; if( argc == 3 ) { // drawImage(image, dx, dy) dx = (float)JSValueToNumberFast(ctx, argv[1]); dy = (float)JSValueToNumberFast(ctx, argv[2]); sw = image?image->width:0; sh = image?image->height:0; dw = sw / scale; dh = sh / scale; } else if( argc == 5 ) { // drawImage(image, dx, dy, dw, dh) dx = (float)JSValueToNumberFast(ctx, argv[1]); dy = (float)JSValueToNumberFast(ctx, argv[2]); dw = (float)JSValueToNumberFast(ctx, argv[3]); dh = (float)JSValueToNumberFast(ctx, argv[4]); sw = image?image->width:0; sh = image?image->height:0; } else if( argc >= 9 ) { // drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh) sx = (short)(JSValueToNumberFast(ctx, argv[1]) * scale); sy = (short)(JSValueToNumberFast(ctx, argv[2]) * scale); sw = (short)(JSValueToNumberFast(ctx, argv[3]) * scale); sh = (short)(JSValueToNumberFast(ctx, argv[4]) * scale); dx = (float)JSValueToNumberFast(ctx, argv[5]); dy = (float)JSValueToNumberFast(ctx, argv[6]); dw = (float)JSValueToNumberFast(ctx, argv[7]); dh = (float)JSValueToNumberFast(ctx, argv[8]); } else { return NULL; } //ejectaInstance->currentRenderingContext = renderingContext; ejectaInstance->setCurrentRenderingContext(renderingContext); renderingContext->drawImage(image, sx, sy, sw, sh, dx, dy, dw, dh); // [renderingContext drawImage:image sx:sx sy:sy sw:sw sh:sh dx:dx dy:dy dw:dw dh:dh]; return NULL; }
/** * seed_exception_get_message: * @ctx: A #SeedContext. * @exception: A reference to a #SeedException. * * Retrieves the message of the given exception; this should be a * human-readable string describing the exception enough that a developer * could utilize the message in order to determine where to look to debug * the problem. * * Return value: A #gchar* representing the detailed message of @exception. * */ gchar * seed_exception_get_message (JSContextRef ctx, JSValueRef e) { JSValueRef name; g_assert ((e)); if (!JSValueIsObject (ctx, e)) return 0; name = seed_object_get_property (ctx, (JSObjectRef) e, "message"); return seed_value_to_string (ctx, name, NULL); }
JSValueRef LayoutTestController::computedStyleIncludingVisitedInfo(JSValueRef element) { // FIXME: Is it OK this works only for the main frame? WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page()); JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame); if (!JSValueIsObject(context, element)) return JSValueMakeUndefined(context); JSValueRef value = WKBundleFrameGetComputedStyleIncludingVisitedInfo(mainFrame, const_cast<JSObjectRef>(element)); if (!value) return JSValueMakeUndefined(context); return value; }
bool JS4D::ThrowVErrorForException( ContextRef inContext, ExceptionRef inException) { bool errorThrown; if ( (inException != NULL) && !JSValueIsUndefined( inContext, inException)) { VTask::GetCurrent()->GetDebugContext().DisableUI(); bool bHasBeenAborted = false; // first throw an error for javascript VString description; if (ValueToString( inContext, inException, description, /*outException*/ NULL)) { if ( description. BeginsWith ( CVSTR ( "SyntaxError" ) ) && JSValueIsObject ( inContext, inException ) ) // Just to be super safe at the moment to avoid breaking everything { JSObjectRef jsExceptionObject = JSValueToObject ( inContext, inException, NULL ); if ( jsExceptionObject != 0 ) { JSStringRef jsLine = JSStringCreateWithUTF8CString ( "line" ); if ( JSObjectHasProperty ( inContext, jsExceptionObject, jsLine ) ) { JSValueRef jsValLine = JSObjectGetProperty ( inContext, jsExceptionObject, jsLine, NULL ); if ( jsValLine != 0 ) { double nLine = JSValueToNumber( inContext, jsValLine, NULL ); VString vstrLine ( "Error on line " ); vstrLine. AppendReal ( nLine ); vstrLine. AppendCString ( ". " ); description. Insert ( vstrLine, 1 ); } } JSStringRelease ( jsLine ); } } else if ( description. Find ( CVSTR ( "Error: Execution aborted by caller." ) ) > 0 && JSValueIsObject ( inContext, inException ) ) bHasBeenAborted = true; if ( !bHasBeenAborted ) { StThrowError<> error( MAKE_VERROR( 'JS4D', 1)); error->SetString( "error_description", description); } } VTask::GetCurrent()->GetDebugContext().EnableUI(); errorThrown = !bHasBeenAborted; } else { errorThrown = false; } return errorThrown; }
static string dumpPath(WKBundlePageRef page, WKBundleScriptWorldRef world, WKBundleNodeHandleRef node) { if (!node) return "(null)"; WKBundleFrameRef frame = WKBundlePageGetMainFrame(page); JSGlobalContextRef context = WKBundleFrameGetJavaScriptContextForWorld(frame, world); JSValueRef nodeValue = WKBundleFrameGetJavaScriptWrapperForNodeForWorld(frame, node, world); ASSERT(JSValueIsObject(context, nodeValue)); JSObjectRef nodeObject = (JSObjectRef)nodeValue; return dumpPath(context, nodeObject); }
static JSValueRef seed_gobject_signal_connect_by_name (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef * exception) { GType obj_type; JSObjectRef user_data = NULL; gchar *signal_name; GObject *obj; gulong id; if (argumentCount < 2 || argumentCount > 3) { seed_make_exception (ctx, exception, "ArgumentError", "Signal connection expected" " 2 or 3 arguments. Got " "%zd", argumentCount); return JSValueMakeNull (ctx); } if (JSValueIsNull (ctx, arguments[1]) || !JSValueIsObject (ctx, arguments[1]) || !JSObjectIsFunction (ctx, (JSObjectRef) arguments[1])) { seed_make_exception (ctx, exception, "ArgumentError", "Signal connection by name " "requires a function" " as second argument"); return JSValueMakeNull (ctx); } if (argumentCount == 3) { user_data = (JSObjectRef) arguments[2]; } signal_name = seed_value_to_string (ctx, arguments[0], exception); obj = (GObject *) JSObjectGetPrivate (thisObject); obj_type = G_OBJECT_TYPE (obj); id = seed_gobject_signal_connect (ctx, signal_name, obj, (JSObjectRef) arguments[1], NULL, user_data); g_free (signal_name); return seed_value_from_ulong (ctx, id, exception); }
int JSArray::indexOf(const JSValue& val, int start) const { JSValueRef prop = JSObjectGetProperty(ctx_, instance_, JSString("indexOf"), nullptr); if (JSValueIsObject(ctx_, prop)) { JSObjectRef func = JSValueToObject(ctx_, prop, nullptr); if (JSObjectIsFunction(ctx_, func)) { JSValueRef args[2] = { val, JSValueMakeNumber(ctx_, start) }; JSValueRef result = JSObjectCallAsFunction(ctx_, func, instance_, 2, args, nullptr); if (JSValueIsNumber(ctx_, result)) { return static_cast<int>(JSValueToNumber(ctx_, result, nullptr)); } } } return -1; }
/** * internal * * implementation of console.log */ static JSValueRef HyperloopLogger (JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { if (argumentCount>0) { std::ostringstream stream; for (size_t c=0;c<argumentCount;c++) { if (JSValueIsObject(ctx,arguments[c]) || JSValueIsString(ctx,arguments[c])) { std::string str(HyperloopJSValueToStringCopy(ctx,arguments[c],exception)); stream << str; } else if (JSValueIsNumber(ctx,arguments[c])) { double num = JSValueToNumber(ctx,arguments[c],exception); double intpart; if (modf(num, &intpart) == 0.0) { stream << intpart; } else { stream << num; } } else if (JSValueIsBoolean(ctx,arguments[c])) { bool b = JSValueToBoolean(ctx,arguments[c]); stream << (b ? "true":"false"); } else if (JSValueIsNull(ctx,arguments[c])) { stream << "null"; } else if (JSValueIsUndefined(ctx,arguments[c])) { stream << "undefined"; } if (c+1 < argumentCount) { stream << " "; } } // call the platform adapter HyperloopNativeLogger(stream.str().c_str()); } return JSValueMakeUndefined(ctx); }
static string dumpPath(JSGlobalContextRef context, JSObjectRef nodeValue) { JSValueRef nodeNameValue = propertyValue(context, nodeValue, "nodeName"); JSRetainPtr<JSStringRef> jsStringNodeName(Adopt, JSValueToStringCopy(context, nodeNameValue, 0)); WKRetainPtr<WKStringRef> nodeName = toWK(jsStringNodeName); JSValueRef parentNode = propertyValue(context, nodeValue, "parentNode"); ostringstream out; out << nodeName; if (parentNode && JSValueIsObject(context, parentNode)) out << " > " << dumpPath(context, (JSObjectRef)parentNode); return out.str(); }
static JSValueRef JSOSInstaller_setDoneHookFunc(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { if (!JSValueIsObjectOfClass(context, thisObject, JSOSInstaller_class(context))) { JSStringRef message = JSStringCreateWithUTF8CString("TypeError: SetDoneHookFunc can only be called on JSOSInstaller"); *exception = JSValueMakeString(context, message); JSStringRelease(message); } else if (argumentCount < 1 || !JSValueIsObject(context, arguments[0])) { JSStringRef message = JSStringCreateWithUTF8CString("TypeError: first argument to SetDoneHookFunc must be a callable"); *exception = JSValueMakeString(context, message); JSStringRelease(message); } else { JSInstaller* jsinst = JSObjectGetPrivate(thisObject); jsinst->done_func = JSValueToObject(context,arguments[0],exception); } return JSValueMakeUndefined(context); }
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); }
KValueRef KKJSMethod::Call(KObjectRef thisObject, const ValueList& args) { JSValueRef thisObjectValue = KJSUtil::ToJSValue(Value::NewObject(thisObject), this->context); if (!JSValueIsObject(this->context, thisObjectValue)) { SharedString ss(thisObject->DisplayString()); throw ValueException::FromFormat("Could not convert %s to JSObjectRef for KKJSMethod::Call", ss->c_str()); } JSObjectRef jsThisObject = JSValueToObject(this->context, thisObjectValue, NULL); if (!jsThisObject) { SharedString ss(thisObject->DisplayString()); throw ValueException::FromFormat("Could not convert %s to JSObjectRef for KKJSMethod::Call", ss->c_str()); } return this->Call(jsThisObject, args); }
static void callOnMessage(JSObjectRef object, WKStringRef contents, WKBundlePageRef page) { static JSStringRef onmessageName = JSStringCreateWithUTF8CString("onmessage"); if (!object) return; WKBundleFrameRef frame = WKBundlePageGetMainFrame(page); JSGlobalContextRef context = WKBundleFrameGetJavaScriptContext(frame); JSValueRef onmessageValue = JSObjectGetProperty(context, object, onmessageName, 0); if (!JSValueIsObject(context, onmessageValue)) return; JSObjectRef onmessageFunction = JSValueToObject(context, onmessageValue, 0); if (!JSObjectIsFunction(context, onmessageFunction)) return; JSObjectRef wrappedMessage = createWrappedMessage(context, contents); JSObjectCallAsFunction(context, onmessageFunction, 0, 1, &wrappedMessage, 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); }