JSValue JSObject::CallAsFunction(const std::vector<JSValue>& arguments, JSObject this_object) { HAL_JSOBJECT_LOCK_GUARD; if (!IsFunction()) { detail::ThrowRuntimeError("JSObject", "This JavaScript object is not a function."); } JSValueRef exception { nullptr }; JSValueRef js_value_ref { nullptr }; if (!arguments.empty()) { const auto arguments_array = detail::to_vector(arguments); js_value_ref = JSObjectCallAsFunction(static_cast<JSContextRef>(js_context__), js_object_ref__, static_cast<JSObjectRef>(this_object), arguments_array.size(), &arguments_array[0], &exception); } else { js_value_ref = JSObjectCallAsFunction(static_cast<JSContextRef>(js_context__), js_object_ref__, static_cast<JSObjectRef>(this_object), 0, nullptr, &exception); } if (exception) { // If this assert fails then we need to JSValueUnprotect // js_value_ref. assert(!js_value_ref); detail::ThrowRuntimeError("JSObject", JSValue(js_context__, exception)); } assert(js_value_ref); return JSValue(js_context__, js_value_ref); }
JNIEXPORT jboolean JNICALL Java_com_appcelerator_hyperloop_ViewOnTouchListener_NativeOnTouch (JNIEnv *env, jobject thiz, jlong jsContextRef, jlong thisObjectRef, jlong onTouchFuncRef, jobject view, jobject event) { JSContextRef ctx = (JSContextRef)jsContextRef; JSObjectRef onTouchFunc = (JSObjectRef)onTouchFuncRef; JSObjectRef thisObject = (JSObjectRef)thisObjectRef; JSValueRef argv[2]; argv[0] = MakeObjectForJava_android_view_View(ctx, view); argv[1] = MakeObjectForJava_android_view_MotionEvent(ctx, event); if (JSObjectIsFunction(ctx, onTouchFunc)) { JSValueRef exception = JSValueMakeNull(ctx); JSValueRef result = JSObjectCallAsFunction(ctx, onTouchFunc, thisObject, 2, argv, &exception); if (!JSValueIsNull(ctx, exception)) { JSStringRef string = JSValueToStringCopy(ctx, exception, NULL); CCHAR_FROM_JSSTRINGREF(string, cstring); LOGD("Java_com_appcelerator_hyperloop_ViewOnTouchListener_NativeOnTouch '%s'", cstring); free(cstring); JSStringRelease(string); } return JSValueToBoolean(ctx, result) ? JNI_TRUE : JNI_FALSE; } return JNI_FALSE; }
/* * 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 EvilExceptionObject_convertToType(JSContextRef context, JSObjectRef object, JSType type, JSValueRef* exception) { UNUSED_PARAM(object); UNUSED_PARAM(exception); JSStringRef funcName; switch (type) { case kJSTypeNumber: funcName = JSStringCreateWithUTF8CString("toNumber"); break; case kJSTypeString: funcName = JSStringCreateWithUTF8CString("toStringExplicit"); break; default: return NULL; break; } JSValueRef func = JSObjectGetProperty(context, object, funcName, exception); JSStringRelease(funcName); JSObjectRef function = JSValueToObject(context, func, exception); if (!function) return NULL; JSValueRef value = JSObjectCallAsFunction(context, function, object, 0, NULL, exception); if (!value) return (JSValueRef)JSStringCreateWithUTF8CString("convertToType failed"); return value; }
JNIEXPORT void JNICALL Java_com_appcelerator_hyperloop_HyperloopJNI_HyperloopCallActivityOnCreate (JNIEnv *env, jobject thiz, jlong jsContextRef, jobject activity, jobject savedInstanceState) { JSContextRef context = (JSContextRef)jsContextRef; JSObjectRef globalObject = JSContextGetGlobalObject(context); JSStringRef onCreate = JSStringCreateWithUTF8CString("onCreate"); JSObjectRef onCreateFunc = JSValueToObject(context, JSObjectGetProperty(context, globalObject, onCreate, NULL), NULL); JSStringRelease(onCreate); // save current Activity JSObjectRef activityObj = MakeObjectForJava_android_app_Activity(context, activity); // save parameter JSValueRef args[1]; args[0] = MakeObjectForJava_android_os_Bundle(context, savedInstanceState); JSValueRef exception = JSValueMakeNull(context); // Call onCreate function if (JSObjectIsFunction(context, onCreateFunc)) { JSObjectCallAsFunction(context, onCreateFunc, activityObj, 1, args, &exception); } if (!JSValueIsNull(context, exception)) { JSStringRef string = JSValueToStringCopy(context, exception, NULL); CCHAR_FROM_JSSTRINGREF(string, cstring); LOGD("Java_com_appcelerator_hyperloop_HyperloopJNI_HyperloopCallActivityOnCreate '%s'", cstring); free(cstring); JSStringRelease(string); } }
void InspectorController::addScriptConsoleMessage(const ConsoleMessage* message) { ASSERT_ARG(message, message); JSStringRef messageConstructorString = JSStringCreateWithUTF8CString("ConsoleMessage"); JSObjectRef messageConstructor = JSValueToObject(m_scriptContext, JSObjectGetProperty(m_scriptContext, m_scriptObject, messageConstructorString, 0), 0); JSStringRelease(messageConstructorString); JSStringRef addMessageString = JSStringCreateWithUTF8CString("addMessageToConsole"); JSObjectRef addMessage = JSValueToObject(m_scriptContext, JSObjectGetProperty(m_scriptContext, m_scriptObject, addMessageString, 0), 0); JSStringRelease(addMessageString); JSValueRef sourceValue = JSValueMakeNumber(m_scriptContext, message->source); JSValueRef levelValue = JSValueMakeNumber(m_scriptContext, message->level); JSStringRef messageString = JSStringCreateWithCharacters(message->message.characters(), message->message.length()); JSValueRef messageValue = JSValueMakeString(m_scriptContext, messageString); JSValueRef lineValue = JSValueMakeNumber(m_scriptContext, message->line); JSStringRef urlString = JSStringCreateWithCharacters(message->url.characters(), message->url.length()); JSValueRef urlValue = JSValueMakeString(m_scriptContext, urlString); JSValueRef args[] = { sourceValue, levelValue, messageValue, lineValue, urlValue }; JSObjectRef messageObject = JSObjectCallAsConstructor(m_scriptContext, messageConstructor, 5, args, 0); JSStringRelease(messageString); JSStringRelease(urlString); JSObjectCallAsFunction(m_scriptContext, addMessage, m_scriptObject, 1, &messageObject, 0); }
JSValueRef EJApp::invokeCallback(JSObjectRef callback, JSObjectRef thisObject, size_t argc, const JSValueRef argv[]) { JSValueRef exception = NULL; JSValueRef result = JSObjectCallAsFunction( jsGlobalContext, callback, thisObject, argc, argv, &exception ); logException(exception,jsGlobalContext); return result; }
static gboolean gum_emit_malloc_range (const GumMallocRangeDetails * details, gpointer user_data) { GumJscMatchContext * mc = user_data; GumJscCore * core = mc->self->core; GumJscScope scope = GUM_JSC_SCOPE_INIT (core); JSContextRef ctx = mc->ctx; JSObjectRef range; JSValueRef result; gboolean proceed; gchar * str; range = JSObjectMake (ctx, NULL, NULL); _gumjs_object_set_pointer (ctx, range, "base", GSIZE_TO_POINTER (details->range->base_address), core); _gumjs_object_set_uint (ctx, range, "size", details->range->size); result = JSObjectCallAsFunction (ctx, mc->on_match, NULL, 1, (JSValueRef *) &range, &scope.exception); _gum_jsc_scope_flush (&scope); proceed = TRUE; if (result != NULL && _gumjs_string_try_get (ctx, result, &str, NULL)) { proceed = strcmp (str, "stop") != 0; g_free (str); } return proceed; }
NATIVE(JSObject,jobject,callAsFunction) (PARAMS, jlong ctx, jlong object, jlong thisObject, jlongArray args) { JSValueRef exception = NULL; int i, sum = 0; jsize len = env->GetArrayLength(args); jlong *values = env->GetLongArrayElements(args, 0); JSValueRef* elements = new JSValueRef[len]; for (i=0; i<len; i++) { elements[i] = (JSValueRef) values[i]; } env->ReleaseLongArrayElements(args, values, 0); jclass ret = env->FindClass("org/liquidplayer/webkit/javascriptcore/JNIReturnObject"); jmethodID cid = env->GetMethodID(ret,"<init>","()V"); jobject out = env->NewObject(ret, cid); jfieldID fid = env->GetFieldID(ret , "reference", "J"); env->SetLongField( out, fid, (long) JSObjectCallAsFunction((JSContextRef) ctx, (JSObjectRef)object, (JSObjectRef)thisObject, (size_t)len, (len==0)?NULL:elements, &exception)); fid = env->GetFieldID(ret , "exception", "J"); env->SetLongField( out, fid, (long) exception); delete elements; return out; }
static gboolean gum_emit_thread (const GumThreadDetails * details, gpointer user_data) { GumJscMatchContext * mc = user_data; GumJscCore * core = mc->self->core; GumJscScope scope = GUM_JSC_SCOPE_INIT (core); JSContextRef ctx = mc->ctx; JSObjectRef thread; JSValueRef result; gboolean proceed; gchar * str; thread = JSObjectMake (ctx, NULL, NULL); _gumjs_object_set_uint (ctx, thread, "id", details->id); _gumjs_object_set_string (ctx, thread, "state", _gumjs_thread_state_to_string (details->state)); _gumjs_object_set (ctx, thread, "context", _gumjs_cpu_context_new (ctx, (GumCpuContext *) &details->cpu_context, GUM_CPU_CONTEXT_READONLY, core)); result = JSObjectCallAsFunction (ctx, mc->on_match, NULL, 1, (JSValueRef *) &thread, &scope.exception); _gum_jsc_scope_flush (&scope); proceed = TRUE; if (result != NULL && _gumjs_string_try_get (ctx, result, &str, NULL)) { proceed = strcmp (str, "stop") != 0; g_free (str); } return proceed; }
bool JS4D::DateObjectToVTime( ContextRef inContext, ObjectRef inObject, VTime& outTime, ExceptionRef *outException) { // it's caller responsibility to check inObject is really a Date using ValueIsInstanceOf // call getTime() bool ok = false; JSStringRef jsString = JSStringCreateWithUTF8CString( "getTime"); JSValueRef getTime = JSObjectGetProperty( inContext, inObject, jsString, outException); JSObjectRef getTimeFunction = JSValueToObject( inContext, getTime, outException); JSStringRelease( jsString); JSValueRef result = (getTime != NULL) ? JSObjectCallAsFunction( inContext, getTimeFunction, inObject, 0, NULL, outException) : NULL; if (result != NULL) { // The getTime() method returns the number of milliseconds since midnight of January 1, 1970. double r = JSValueToNumber( inContext, result, outException); sLONG8 n = (sLONG8) r; if (n == r) { outTime.FromUTCTime( 1970, 1, 1, 0, 0, 0, 0); outTime.AddMilliseconds( n); ok = true; } else { outTime.SetNull( true); } } else { outTime.SetNull( true); } return ok; }
SharedValue KKJSMethod::Call(const ValueList& args) { JSValueRef* js_args = new JSValueRef[args.size()]; for (int i = 0; i < (int) args.size(); i++) { SharedValue arg = args.at(i); js_args[i] = KJSUtil::ToJSValue(arg, this->context); } JSValueRef exception = NULL; JSValueRef js_value = JSObjectCallAsFunction( this->context, this->object, this->this_obj, args.size(), js_args, &exception); delete [] js_args; // clean up args if (js_value == NULL && exception != NULL) //exception thrown { SharedValue tv_exp = KJSUtil::ToKrollValue(exception, this->context, NULL); throw ValueException(tv_exp); } return KJSUtil::ToKrollValue(js_value, this->context, NULL); }
static bool gumjs_proxy_set_property (JSContextRef ctx, JSObjectRef object, JSStringRef property_name, JSValueRef value, JSValueRef * exception) { GumJscProxy * self; GumJscCore * core; self = GUMJS_PROXY (object); if (self->set == NULL) return false; core = JSObjectGetPrivate (JSContextGetGlobalObject (ctx)); { GumJscScope scope = GUM_JSC_SCOPE_INIT (core); JSValueRef argv[3]; JSValueRef result; argv[0] = object; argv[1] = JSValueMakeString (ctx, property_name); argv[2] = value; result = JSObjectCallAsFunction (ctx, self->set, self->receiver, G_N_ELEMENTS (argv), argv, &scope.exception); _gum_jsc_scope_flush (&scope); return true; } }
void InspectorController::focusNode() { if (!enabled()) return; ASSERT(m_scriptContext); ASSERT(m_scriptObject); ASSERT(m_nodeToFocus); JSValueRef arg0; { KJS::JSLock lock; arg0 = toRef(toJS(toJS(m_scriptContext), m_nodeToFocus.get())); } m_nodeToFocus = 0; JSStringRef functionProperty = JSStringCreateWithUTF8CString("updateFocusedNode"); JSObjectRef function = JSValueToObject(m_scriptContext, JSObjectGetProperty(m_scriptContext, m_scriptObject, functionProperty, 0), 0); JSStringRelease(functionProperty); ASSERT(function); JSObjectCallAsFunction(m_scriptContext, function, m_scriptObject, 1, &arg0, 0); }
static gboolean gum_jsc_exception_handler_on_exception (GumExceptionDetails * details, gpointer user_data) { GumJscExceptionHandler * handler = user_data; GumJscCore * core = handler->core; GumJscScope scope; JSContextRef ctx = core->ctx; JSObjectRef exception, cpu_context; JSValueRef result; gboolean handled; _gum_jsc_scope_enter (&scope, core); _gumjs_parse_exception_details (ctx, details, core, &exception, &cpu_context); result = JSObjectCallAsFunction (ctx, handler->callback, NULL, 1, (JSValueRef *) &exception, &scope.exception); _gumjs_cpu_context_detach (cpu_context); handled = FALSE; if (result != NULL) _gumjs_boolean_try_get (ctx, result, &handled, NULL); _gum_jsc_scope_leave (&scope); return handled; }
inline JSValueRef jsc::Function::call(JSContextRef ctx, const JSObjectRef &function, const JSObjectRef &this_object, size_t argc, const JSValueRef arguments[]) { JSValueRef exception = nullptr; JSValueRef result = JSObjectCallAsFunction(ctx, function, this_object, argc, arguments, &exception); if (exception) { throw jsc::Exception(ctx, exception); } return result; }
JSObjectRef InspectorController::addScriptResource(InspectorResource* resource) { ASSERT_ARG(resource, resource); // This happens for pages loaded from the back/forward cache. if (resource->scriptObject) return resource->scriptObject; ASSERT(m_scriptContext); ASSERT(m_scriptObject); if (!m_scriptContext || !m_scriptObject) return 0; JSStringRef resourceString = JSStringCreateWithUTF8CString("Resource"); JSObjectRef resourceConstructor = JSValueToObject(m_scriptContext, JSObjectGetProperty(m_scriptContext, m_scriptObject, resourceString, 0), 0); JSStringRelease(resourceString); String urlString = resource->requestURL.url(); JSStringRef url = JSStringCreateWithCharacters(urlString.characters(), urlString.length()); JSValueRef urlValue = JSValueMakeString(m_scriptContext, url); JSStringRelease(url); urlString = resource->requestURL.host(); JSStringRef domain = JSStringCreateWithCharacters(urlString.characters(), urlString.length()); JSValueRef domainValue = JSValueMakeString(m_scriptContext, domain); JSStringRelease(domain); urlString = resource->requestURL.path(); JSStringRef path = JSStringCreateWithCharacters(urlString.characters(), urlString.length()); JSValueRef pathValue = JSValueMakeString(m_scriptContext, path); JSStringRelease(path); urlString = resource->requestURL.lastPathComponent(); JSStringRef lastPathComponent = JSStringCreateWithCharacters(urlString.characters(), urlString.length()); JSValueRef lastPathComponentValue = JSValueMakeString(m_scriptContext, lastPathComponent); JSStringRelease(lastPathComponent); JSValueRef identifier = JSValueMakeNumber(m_scriptContext, resource->identifier); JSValueRef mainResource = JSValueMakeBoolean(m_scriptContext, m_mainResource == resource); JSValueRef cached = JSValueMakeBoolean(m_scriptContext, resource->cached); JSValueRef arguments[] = { scriptObjectForRequest(m_scriptContext, resource), urlValue, domainValue, pathValue, lastPathComponentValue, identifier, mainResource, cached }; JSObjectRef result = JSObjectCallAsConstructor(m_scriptContext, resourceConstructor, 8, arguments, 0); resource->setScriptObject(m_scriptContext, result); ASSERT(result); JSStringRef addResourceString = JSStringCreateWithUTF8CString("addResource"); JSObjectRef addResourceFunction = JSValueToObject(m_scriptContext, JSObjectGetProperty(m_scriptContext, m_scriptObject, addResourceString, 0), 0); JSStringRelease(addResourceString); JSValueRef addArguments[] = { result }; JSObjectCallAsFunction(m_scriptContext, addResourceFunction, m_scriptObject, 1, addArguments, 0); return result; }
Value Object::callAsFunction(int nArgs, JSValueRef args[]) { JSValueRef exn; JSValueRef result = JSObjectCallAsFunction(m_context, m_obj, NULL, nArgs, args, &exn); if (!result) { std::string exceptionText = Value(m_context, exn).toString().str(); throwJSExecutionException("Exception calling JS function: %s", exceptionText.c_str()); } return Value(m_context, result); }
static void callLayoutTestControllerCallback(unsigned index) { if (!callbackMap().contains(index)) return; WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(InjectedBundle::shared().page()->page()); JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame); JSObjectRef callback = JSValueToObject(context, callbackMap().take(index), 0); JSObjectCallAsFunction(context, callback, JSContextGetGlobalObject(context), 0, 0, 0); JSValueUnprotect(context, callback); }
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); } } }
/** * 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); }
bool web_view_callback (struct js_callback_data *data) { unsigned short i; JSValueRef js_args[data->args_len]; for (i = 0; i < data->args_len; i++) { switch (data->args[i].type) { case kJSTypeBoolean: js_args[i] = JSValueMakeBoolean(data->widget->js_context, data->args[i].value.boolean); break; case kJSTypeNull: js_args[i] = JSValueMakeNull(data->widget->js_context); break; case kJSTypeNumber: js_args[i] = JSValueMakeNumber(data->widget->js_context, data->args[i].value.number); break; case kJSTypeObject: js_args[i] = data->args[i].value.object; break; case kJSTypeString: { JSStringRef str = JSStringCreateWithUTF8CString(data->args[i].value.string); js_args[i] = JSValueMakeString(data->widget->js_context, str); JSStringRelease(str); break; } case kJSTypeUndefined: js_args[i] = JSValueMakeUndefined(data->widget->js_context); break; } } if (!data->widget->js_context || !data->widget->js_object) { LOG_ERR("missing JS context or object!"); return false; } JSStringRef str_ondatachanged = JSStringCreateWithUTF8CString("onDataChanged"); JSValueRef func = JSObjectGetProperty(data->widget->js_context, data->widget->js_object, str_ondatachanged, NULL); JSObjectRef function = JSValueToObject(data->widget->js_context, func, NULL); JSStringRelease(str_ondatachanged); /* let the thread know we're done with the data so it can cleanup */ pthread_cond_signal(&update_cond); if (!JSObjectIsFunction(data->widget->js_context, function)) { LOG_DEBUG("onDataChanged callback for 'widget_%s' with type '%s' is not a function or is not set", data->widget->name, data->widget->type); return false; /* only run once */ } JSObjectCallAsFunction(data->widget->js_context, function, NULL, data->args_len, js_args, NULL); return false; /* only run once */ }
static void callClearFunction(JSContextRef context, JSObjectRef thisObject, const char* functionName) { ASSERT_ARG(context, context); ASSERT_ARG(thisObject, thisObject); JSStringRef string = JSStringCreateWithUTF8CString(functionName); JSObjectRef function = JSValueToObject(context, JSObjectGetProperty(context, thisObject, string, 0), 0); JSStringRelease(string); JSObjectCallAsFunction(context, function, thisObject, 0, 0, 0); }
void JSOSInstaller_setDone(JSGlobalContextRef context,JSInstaller* jsinst) { ///JS_EXPORT JSValueRef JSObjectCallAsFunction(JSContextRef ctx, JSObjectRef object, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception); log_msg("start"); if(jsinst->js_self && jsinst->done_func){ JSValueRef arguments[]={jsinst->js_self}; JSObjectCallAsFunction(context,jsinst->done_func,NULL,1,arguments,NULL); } log_msg("end"); }
bool IsArray(JSObjectRef obj) { if (g_fnxIsArray == NULL) { JSStringRef fnScript = JSStringCreateWithUTF8CString("return arguments[0] instanceof Array"); g_fnxIsArray = JSObjectMakeFunction(g_ctx, NULL, 0, NULL, fnScript, NULL, 0, NULL); JSValueProtect(g_ctx, g_fnxIsArray); JSStringRelease(fnScript); } JSValueRef isArray = JSObjectCallAsFunction(g_ctx, g_fnxIsArray, NULL, 1, (JSValueRef*) &obj, NULL); return JSValueToBoolean(g_ctx, isArray); }
void do_run_timeout(void *data) { struct timeout_data_t *timeout_data = data; JSValueRef args[1]; args[0] = timeout_data_to_js_value(ctx, timeout_data); free(timeout_data); JSObjectRef run_timeout = cljs_get_function("global", "PLANCK_RUN_TIMEOUT"); cljs_acquire_eval_lock(); JSObjectCallAsFunction(ctx, run_timeout, NULL, 1, args, NULL); cljs_release_eval_lock(); }
void jsmessage(int signum) { JSValueRef arguments[2]; JSValueRef result; int num_arguments = 2; JSStringRef myFunctionName = JSStringCreateWithUTF8CString("my_function"); JSObjectRef functionObject = (JSObjectRef)JSObjectGetProperty(context, globalObject, myFunctionName, NULL); arguments[0] = JSValueMakeNumber(context, signum); arguments[1] = JSValueMakeNumber(context, 3.14); result = JSObjectCallAsFunction(context, functionObject, globalObject, num_arguments, arguments, NULL); syslog (LOG_NOTICE, "After SIG %d", signum); }
void test_function(JSContextRef ctx, JSValueRef jsthis) { const char *script = "function a() { return this; }; a;"; JSStringRef jsscript = JSStringCreateWithUTF8CString(script); JSValueRef jsret = JSEvaluateScript(ctx, jsscript, NULL, NULL, 0, NULL); JSObjectRef jsfun = (JSObjectRef)jsret; jsret = (JSValueRef)JSObjectCallAsFunction(ctx, jsfun, (JSObjectRef)jsthis, 0, NULL, NULL); JSStringRelease(jsscript); printf("%lx\n", (unsigned long)jsret); print_js(ctx, jsret); }
static JSValueRef search(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* /*exception*/) { InspectorController* controller = reinterpret_cast<InspectorController*>(JSObjectGetPrivate(thisObject)); if (!controller) return JSValueMakeUndefined(ctx); if (argumentCount < 2 || !JSValueIsString(ctx, arguments[1])) return JSValueMakeUndefined(ctx); Node* node = toNode(toJS(arguments[0])); if (!node) return JSValueMakeUndefined(ctx); JSStringRef string = JSValueToStringCopy(ctx, arguments[1], 0); String target(JSStringGetCharactersPtr(string), JSStringGetLength(string)); JSStringRelease(string); JSObjectRef globalObject = JSContextGetGlobalObject(ctx); JSStringRef constructorString = JSStringCreateWithUTF8CString("Array"); JSObjectRef arrayConstructor = JSValueToObject(ctx, JSObjectGetProperty(ctx, globalObject, constructorString, 0), 0); JSStringRelease(constructorString); JSObjectRef array = JSObjectCallAsConstructor(ctx, arrayConstructor, 0, 0, 0); JSStringRef pushString = JSStringCreateWithUTF8CString("push"); JSValueRef pushValue = JSObjectGetProperty(ctx, array, pushString, 0); JSStringRelease(pushString); JSObjectRef push = JSValueToObject(ctx, pushValue, 0); RefPtr<Range> searchRange(rangeOfContents(node)); int exception = 0; do { RefPtr<Range> resultRange(findPlainText(searchRange.get(), target, true, false)); if (resultRange->collapsed(exception)) break; // A non-collapsed result range can in some funky whitespace cases still not // advance the range's start position (4509328). Break to avoid infinite loop. VisiblePosition newStart = endVisiblePosition(resultRange.get(), DOWNSTREAM); if (newStart == startVisiblePosition(searchRange.get(), DOWNSTREAM)) break; KJS::JSLock lock; JSValueRef arg0 = toRef(toJS(toJS(ctx), resultRange.get())); JSObjectCallAsFunction(ctx, push, array, 1, &arg0, 0); setStart(searchRange.get(), newStart); } while (true); return array; }
template<> std::string RJSAccessor::to_binary(JSContextRef ctx, JSValueRef &val) { static JSStringRef arrayBufferString = JSStringCreateWithUTF8CString("ArrayBuffer"); static JSStringRef bufferString = JSStringCreateWithUTF8CString("buffer"); static JSStringRef byteLengthString = JSStringCreateWithUTF8CString("byteLength"); static JSStringRef byteOffsetString = JSStringCreateWithUTF8CString("byteOffset"); static JSStringRef isViewString = JSStringCreateWithUTF8CString("isView"); static JSStringRef uint8ArrayString = JSStringCreateWithUTF8CString("Uint8Array"); JSObjectRef arrayBufferConstructor = RJSValidatedObjectProperty(ctx, JSContextGetGlobalObject(ctx), arrayBufferString); JSObjectRef uint8ArrayContructor = RJSValidatedObjectProperty(ctx, JSContextGetGlobalObject(ctx), uint8ArrayString); JSValueRef uint8ArrayArguments[3]; size_t uint8ArrayArgumentsCount = 0; // Value should either be an ArrayBuffer or ArrayBufferView (i.e. TypedArray or DataView). if (JSValueIsInstanceOfConstructor(ctx, val, arrayBufferConstructor, NULL)) { uint8ArrayArguments[0] = val; uint8ArrayArgumentsCount = 1; } else if (JSObjectRef object = JSValueToObject(ctx, val, NULL)) { // Check if value is an ArrayBufferView by calling ArrayBuffer.isView(val). JSObjectRef isViewMethod = RJSValidatedObjectProperty(ctx, arrayBufferConstructor, isViewString); JSValueRef isView = JSObjectCallAsFunction(ctx, isViewMethod, arrayBufferConstructor, 1, &val, NULL); if (isView && JSValueToBoolean(ctx, isView)) { uint8ArrayArguments[0] = RJSValidatedObjectProperty(ctx, object, bufferString); uint8ArrayArguments[1] = RJSValidatedPropertyValue(ctx, object, byteOffsetString); uint8ArrayArguments[2] = RJSValidatedPropertyValue(ctx, object, byteLengthString); uint8ArrayArgumentsCount = 3; } } if (!uint8ArrayArgumentsCount) { throw std::runtime_error("Can only convert ArrayBuffer and TypedArray objects to binary"); } JSValueRef exception = NULL; JSObjectRef uint8Array = JSObjectCallAsConstructor(ctx, uint8ArrayContructor, uint8ArrayArgumentsCount, uint8ArrayArguments, &exception); if (exception) { throw RJSException(ctx, exception); } size_t byteCount = RJSValidatedListLength(ctx, uint8Array); std::string bytes(byteCount, 0); for (size_t i = 0; i < byteCount; i++) { JSValueRef byteValue = JSObjectGetPropertyAtIndex(ctx, uint8Array, (unsigned)i, NULL); bytes[i] = JSValueToNumber(ctx, byteValue, NULL); } return bytes; }