static gboolean element_text_equal_to(JSContextRef context, const gchar* text) { JSStringRef scriptString = JSStringCreateWithUTF8CString( "window.document.getElementById(\"in\").value;"); JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0); JSStringRelease(scriptString); // If the value isn't a string, the element is probably a div // so grab the innerText instead. if (!JSValueIsString(context, value)) { JSStringRef scriptString = JSStringCreateWithUTF8CString( "window.document.getElementById(\"in\").innerText;"); value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0); JSStringRelease(scriptString); } g_assert(JSValueIsString(context, value)); JSStringRef inputString = JSValueToStringCopy(context, value, 0); g_assert(inputString); gint size = JSStringGetMaximumUTF8CStringSize(inputString); gchar* cString = g_malloc(size); JSStringGetUTF8CString(inputString, cString, size); JSStringRelease(inputString); gboolean result = g_utf8_collate(cString, text) == 0; g_free(cString); return result; }
JSValueRef JSCCharacterData::appendDataCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { struct JSCCharacterDataPrivate* privData = (struct JSCCharacterDataPrivate*)JSObjectGetPrivate(thisObj); if (false) { } else if (argumentCount == 1 && JSValueIsString(ctx, arguments[0])) { JSStringRef stringReflocalArg = JSValueToStringCopy(ctx, arguments[0], exception); size_t localArgMaxSize = JSStringGetMaximumUTF8CStringSize(stringReflocalArg); char* localArgBuffer = new char[localArgMaxSize]; JSStringGetUTF8CString(stringReflocalArg, localArgBuffer, localArgMaxSize); std::string localArg(localArgBuffer); JSStringRelease(stringReflocalArg); free(localArgBuffer); privData->nativeObj->appendData(localArg); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling appendData"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
static void web_view_javascript_finished(GObject *object, GAsyncResult *result, gpointer user_data) { WebKitJavascriptResult *js_result; JSValueRef value; JSGlobalContextRef context; GError *error = NULL; js_result = webkit_web_view_run_javascript_finish(WEBKIT_WEB_VIEW(object), result, &error); if (!js_result) { g_warning ("Error running javascript: %s", error->message); g_error_free (error); return; } context = webkit_javascript_result_get_global_context(js_result); value = webkit_javascript_result_get_value(js_result); if (JSValueIsString(context, value)) { JSStringRef js_str_value; gchar *str_value; gsize str_length; js_str_value = JSValueToStringCopy(context, value, NULL); str_length = JSStringGetMaximumUTF8CStringSize(js_str_value); str_value = (gchar *)g_malloc(str_length); JSStringGetUTF8CString(js_str_value, str_value, str_length); JSStringRelease(js_str_value); g_free (str_value); } else { g_warning("Error running javascript: unexpected return value"); } webkit_javascript_result_unref(js_result); }
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; }
bool LayoutTestController::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray) { JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length")); JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); if (!JSValueIsNumber(context, lengthValue)) return false; WebCore::FindOptions options = 0; const size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0)); for (size_t i = 0; i < length; ++i) { JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); if (!JSValueIsString(context, value)) continue; JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0)); if (equals(optionName, "CaseInsensitive")) options |= WebCore::CaseInsensitive; else if (equals(optionName, "AtWordStarts")) options |= WebCore::AtWordStarts; else if (equals(optionName, "TreatMedialCapitalAsWordStart")) options |= WebCore::TreatMedialCapitalAsWordStart; else if (equals(optionName, "Backwards")) options |= WebCore::Backwards; else if (equals(optionName, "WrapAround")) options |= WebCore::WrapAround; else if (equals(optionName, "StartInSelection")) options |= WebCore::StartInSelection; } return DumpRenderTreeSupportEfl::findString(browser->mainView(), target->ustring().utf8().data(), options); }
/** * The callback from JavaScriptCore. We told JSC to call this function * whenever it sees "console.setscript". */ static JSValueRef console_setscript(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { if (!JSValueIsObjectOfClass(ctx, thisObject, ConsoleClass())) return JSValueMakeUndefined(ctx); if (argumentCount < 1) return JSValueMakeUndefined(ctx); // Convert the result into a string for display. if (!JSValueIsString(ctx, arguments[0])) return JSValueMakeUndefined(ctx); JSStringRef temp = JSValueToStringCopy (ctx, arguments[0], exception); if (exception && *exception) return JSValueMakeUndefined(ctx); BSTR strTemp = ::SysAllocString(JSStringGetCharactersPtr(temp)); JSStringRelease(temp); CallJSDlg* dlg = static_cast<CallJSDlg*>(JSObjectGetPrivate(thisObject)); dlg->setScript(strTemp); ::SysFreeString(strTemp); return JSValueMakeUndefined(ctx); }
/** * The callback from JavaScriptCore. We told JSC to call this function * whenever it sees "console.log". */ static JSValueRef console_log(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { if (!JSValueIsObjectOfClass(ctx, thisObject, ConsoleClass())) return JSValueMakeUndefined(ctx); if (argumentCount < 1) return JSValueMakeUndefined(ctx); // Convert the result into a string for display. if (!JSValueIsString(ctx, arguments[0])) return JSValueMakeUndefined(ctx); JSStringRef temp = JSValueToStringCopy (ctx, arguments[0], exception); if (exception && *exception) return JSValueMakeUndefined(ctx); BSTR strTemp = ::SysAllocString(JSStringGetCharactersPtr(temp)); JSStringRelease(temp); ::OutputDebugString(strTemp); ::OutputDebugString(_T("\r\n")); ::SysFreeString(strTemp); return JSValueMakeUndefined(ctx); }
bool TestRunner::findString(JSContextRef context, JSStringRef string, JSObjectRef optionsArray) { JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length")); JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); if (!JSValueIsNumber(context, lengthValue)) return false; QWebPage::FindFlags findFlags = QWebPage::FindCaseSensitively; int length = static_cast<int>(JSValueToNumber(context, lengthValue, 0)); for (int i = 0; i < length; ++i) { JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); if (!JSValueIsString(context, value)) continue; JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0)); if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive")) findFlags &= ~QWebPage::FindCaseSensitively; else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts")) findFlags |= QWebPage::FindAtWordBeginningsOnly; else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart")) findFlags |= QWebPage::TreatMedialCapitalAsWordBeginning; else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards")) findFlags |= QWebPage::FindBackward; else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround")) findFlags |= QWebPage::FindWrapsAroundDocument; else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection")) findFlags |= QWebPage::FindBeginsInSelection; } DumpRenderTree* drt = DumpRenderTree::instance(); return drt->webPage()->findText(JSStringCopyQString(string), findFlags); }
char* jsvalue_to_cstr(JSContextRef ctx, JSValueRef jsvalue) { if (!JSValueIsString(ctx, jsvalue)) { return NULL; } JSStringRef js_string = JSValueToStringCopy(ctx, jsvalue, NULL); char* cstr = jsstring_to_cstr(ctx, js_string); JSStringRelease(js_string); return cstr; }
JSValueRef function_print_err_fn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueIsString(ctx, args[0])) { char *str = value_to_c_string(ctx, args[0]); fprintf(stderr, "%s", str); fflush(stderr); free(str); } return JSValueMakeNull(ctx); }
static char* jsValueToCString(JSGlobalContextRef context, JSValueRef value) { g_assert(value); g_assert(JSValueIsString(context, value)); JSRetainPtr<JSStringRef> stringValue(Adopt, JSValueToStringCopy(context, value, 0)); g_assert(stringValue); size_t cStringLength = JSStringGetMaximumUTF8CStringSize(stringValue.get()); char* cString = static_cast<char*>(g_malloc(cStringLength)); JSStringGetUTF8CString(stringValue.get(), cString, cStringLength); return cString; }
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; }
/* Sys.print implementation */ static JSValueRef cb_sysclass_print(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) { /* At least, one argument must be received */ if (argumentCount == 1 && JSValueIsString(context, arguments[0])) { /* Converts JSValue to char */ size_t len; char * cstr; JSStringRef jsstr = JSValueToStringCopy(context, arguments[0], NULL); len = JSStringGetMaximumUTF8CStringSize(jsstr); cstr = g_new(char, len); JSStringGetUTF8CString(jsstr, cstr, len); g_print("%s\n\n", cstr); g_free(cstr); JSStringRelease(jsstr); }
static JSValueRef log(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef /*thisObject*/, size_t argumentCount, const JSValueRef arguments[], JSValueRef* /*exception*/) { if (argumentCount < 1 || !JSValueIsString(ctx, arguments[0])) return JSValueMakeUndefined(ctx); #ifndef NDEBUG JSStringRef string = JSValueToStringCopy(ctx, arguments[0], 0); String message(JSStringGetCharactersPtr(string), JSStringGetLength(string)); JSStringRelease(string); fprintf(stderr, "%s\n", message.latin1().data()); #endif return JSValueMakeUndefined(ctx); }
static JSObjectRef MyObject_callAsConstructor(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { UNUSED_PARAM(context); UNUSED_PARAM(object); if (argumentCount > 0 && JSValueIsString(context, arguments[0]) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, arguments[0], 0), "throwOnConstruct")) { JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), object, JSStringCreateWithUTF8CString("test script"), 1, exception); return object; } if (argumentCount > 0 && JSValueIsStrictEqual(context, arguments[0], JSValueMakeNumber(context, 0))) return JSValueToObject(context, JSValueMakeNumber(context, 1), exception); return JSValueToObject(context, JSValueMakeNumber(context, 0), exception); }
/** * 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 JSValueRef qt_postWebChannelMessageCallback(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) { // FIXME: should it work regardless of the thisObject? if (argumentCount < 1 || !JSValueIsString(context, arguments[0])) return JSValueMakeUndefined(context); QtBuiltinBundlePage* bundlePage = reinterpret_cast<QtBuiltinBundlePage*>(JSObjectGetPrivate(thisObject)); ASSERT(bundlePage); // TODO: can we transmit the data as JS object, instead of as a string? JSRetainPtr<JSStringRef> jsContents = JSValueToStringCopy(context, arguments[0], 0); WKRetainPtr<WKStringRef> contents(AdoptWK, WKStringCreateWithJSString(jsContents.get())); bundlePage->postMessageFromNavigatorQtWebChannelTransport(contents.get()); return JSValueMakeUndefined(context); }
static bool MyObject_hasInstance(JSContextRef context, JSObjectRef constructor, JSValueRef possibleValue, JSValueRef* exception) { UNUSED_PARAM(context); UNUSED_PARAM(constructor); if (JSValueIsString(context, possibleValue) && JSStringIsEqualToUTF8CString(JSValueToStringCopy(context, possibleValue, 0), "throwOnHasInstance")) { JSEvaluateScript(context, JSStringCreateWithUTF8CString("throw 'an exception'"), constructor, JSStringCreateWithUTF8CString("test script"), 1, exception); return false; } JSStringRef numberString = JSStringCreateWithUTF8CString("Number"); JSObjectRef numberConstructor = JSValueToObject(context, JSObjectGetProperty(context, JSContextGetGlobalObject(context), numberString, exception), exception); JSStringRelease(numberString); return JSValueIsInstanceOfConstructor(context, possibleValue, numberConstructor, exception); }
static void didRunJavaScript(WKSerializedScriptValueRef resultSerializedScriptValue, WKErrorRef error, void* context) { EXPECT_EQ(reinterpret_cast<void*>(0x1234578), context); EXPECT_NOT_NULL(resultSerializedScriptValue); JSGlobalContextRef scriptContext = JSGlobalContextCreate(0); JSValueRef scriptValue = WKSerializedScriptValueDeserialize(resultSerializedScriptValue, scriptContext, 0); EXPECT_TRUE(JSValueIsString(scriptContext, scriptValue)); // Make sure that the result of navigator.userAgent isn't empty, even if we set the custom // user agent to the empty string. JSStringRef scriptString = JSValueToStringCopy(scriptContext, scriptValue, 0); EXPECT_GT(JSStringGetLength(scriptString), 0u); JSStringRelease(scriptString); JSGlobalContextRelease(scriptContext); testDone = true; }
static JSValueRef runPasteTestCallback(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { gtk_widget_grab_focus(GTK_WIDGET(currentFixture->webView)); // Simulate a paste keyboard sequence. GdkEvent* event = gdk_event_new(GDK_KEY_PRESS); event->key.keyval = gdk_unicode_to_keyval('v'); event->key.state = GDK_CONTROL_MASK; event->key.window = gtk_widget_get_window(GTK_WIDGET(currentFixture->webView)); g_object_ref(event->key.window); #ifndef GTK_API_VERSION_2 GdkDeviceManager* manager = gdk_display_get_device_manager(gdk_window_get_display(event->key.window)); gdk_event_set_device(event, gdk_device_manager_get_client_pointer(manager)); #endif GdkKeymapKey* keys; gint n_keys; if (gdk_keymap_get_entries_for_keyval(gdk_keymap_get_default(), event->key.keyval, &keys, &n_keys)) { event->key.hardware_keycode = keys[0].keycode; g_free(keys); } gtk_main_do_event(event); event->key.type = GDK_KEY_RELEASE; gtk_main_do_event(event); gdk_event_free(event); JSStringRef scriptString = JSStringCreateWithUTF8CString("document.body.innerHTML;"); JSValueRef value = JSEvaluateScript(context, scriptString, 0, 0, 0, 0); JSStringRelease(scriptString); g_assert(JSValueIsString(context, value)); JSStringRef actual = JSValueToStringCopy(context, value, exception); g_assert(!exception || !*exception); g_assert(currentFixture->info->expectedContent); JSStringRef expected = JSStringCreateWithUTF8CString(currentFixture->info->expectedContent); g_assert(JSStringIsEqual(expected, actual)); JSStringRelease(expected); JSStringRelease(actual); g_main_loop_quit(currentFixture->loop); return JSValueMakeUndefined(context); }
static JSValueRef qt_postMessageCallback(JSContextRef context, JSObjectRef, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef*) { // FIXME: should it work regardless of the thisObject? if (argumentCount < 1 || !JSValueIsString(context, arguments[0])) return JSValueMakeUndefined(context); QtBuiltinBundlePage* bundlePage = reinterpret_cast<QtBuiltinBundlePage*>(JSObjectGetPrivate(thisObject)); ASSERT(bundlePage); // FIXME: needed? if (!bundlePage->navigatorQtObjectEnabled()) return JSValueMakeUndefined(context); JSRetainPtr<JSStringRef> jsContents = JSValueToStringCopy(context, arguments[0], 0); WKRetainPtr<WKStringRef> contents(AdoptWK, WKStringCreateWithJSString(jsContents.get())); bundlePage->postMessageFromNavigatorQtObject(contents.get()); return JSValueMakeUndefined(context); }
char *value_to_c_string(JSContextRef ctx, JSValueRef val) { if (JSValueIsNull(ctx, val)) { return NULL; } if (!JSValueIsString(ctx, val)) { #ifdef DEBUG fprintf(stderr, "WARN: not a string\n"); #endif return NULL; } JSStringRef str_ref = JSValueToStringCopy(ctx, val, NULL); size_t len = JSStringGetMaximumUTF8CStringSize(str_ref); char *str = malloc(len * sizeof(char)); JSStringGetUTF8CString(str_ref, str, len); JSStringRelease(str_ref); return str; }
JSValueRef function_print_fn(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (!should_keep_running()) { fprintf(stdout, "\x1b[m\n"); fflush(stdout); *exception = JSValueMakeNull(ctx); return NULL; } if (argc == 1 && JSValueIsString(ctx, args[0])) { char *str = value_to_c_string(ctx, args[0]); fprintf(stdout, "%s", str); fflush(stdout); free(str); } return JSValueMakeNull(ctx); }
static bool console_setSharedValue(JSContextRef ctx, JSObjectRef thisObject, JSStringRef /*propertyName*/, JSValueRef value, JSValueRef* exception) { if (!JSValueIsString(ctx, value)) return false; CallJSDlg* dlg = static_cast<CallJSDlg*>(JSObjectGetPrivate(thisObject)); if (!dlg) return false; JSStringRef temp = JSValueToStringCopy (ctx, value, exception); if (exception && *exception) return false; BSTR strTemp = ::SysAllocString(JSStringGetCharactersPtr(temp)); JSStringRelease(temp); dlg->setSharedString(strTemp); ::SysFreeString(strTemp); return true; }
bool TestRunner::findString(JSStringRef target, JSValueRef optionsArrayAsValue) { WKFindOptions options = 0; auto& injectedBundle = InjectedBundle::singleton(); WKBundleFrameRef mainFrame = WKBundlePageGetMainFrame(injectedBundle.page()->page()); JSContextRef context = WKBundleFrameGetJavaScriptContext(mainFrame); JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length")); JSObjectRef optionsArray = JSValueToObject(context, optionsArrayAsValue, 0); JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); if (!JSValueIsNumber(context, lengthValue)) return false; size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0)); for (size_t i = 0; i < length; ++i) { JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); if (!JSValueIsString(context, value)) continue; JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0)); if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive")) options |= kWKFindOptionsCaseInsensitive; else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts")) options |= kWKFindOptionsAtWordStarts; else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart")) options |= kWKFindOptionsTreatMedialCapitalAsWordStart; else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards")) options |= kWKFindOptionsBackwards; else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround")) options |= kWKFindOptionsWrapAround; else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection")) { // FIXME: No kWKFindOptionsStartInSelection. } } return WKBundlePageFindString(injectedBundle.page()->page(), toWK(target).get(), options); }
bool LayoutTestController::findString(JSContextRef context, JSStringRef target, JSObjectRef optionsArray) { WebKitFindOptions findOptions = 0; WebKitWebView* webView = webkit_web_frame_get_web_view(mainFrame); ASSERT(webView); JSRetainPtr<JSStringRef> lengthPropertyName(Adopt, JSStringCreateWithUTF8CString("length")); JSValueRef lengthValue = JSObjectGetProperty(context, optionsArray, lengthPropertyName.get(), 0); if (!JSValueIsNumber(context, lengthValue)) return false; GOwnPtr<gchar> targetString(JSStringCopyUTF8CString(target)); size_t length = static_cast<size_t>(JSValueToNumber(context, lengthValue, 0)); for (size_t i = 0; i < length; ++i) { JSValueRef value = JSObjectGetPropertyAtIndex(context, optionsArray, i, 0); if (!JSValueIsString(context, value)) continue; JSRetainPtr<JSStringRef> optionName(Adopt, JSValueToStringCopy(context, value, 0)); if (JSStringIsEqualToUTF8CString(optionName.get(), "CaseInsensitive")) findOptions |= WebKit::WebFindOptionsCaseInsensitive; else if (JSStringIsEqualToUTF8CString(optionName.get(), "AtWordStarts")) findOptions |= WebKit::WebFindOptionsAtWordStarts; else if (JSStringIsEqualToUTF8CString(optionName.get(), "TreatMedialCapitalAsWordStart")) findOptions |= WebKit::WebFindOptionsTreatMedialCapitalAsWordStart; else if (JSStringIsEqualToUTF8CString(optionName.get(), "Backwards")) findOptions |= WebKit::WebFindOptionsBackwards; else if (JSStringIsEqualToUTF8CString(optionName.get(), "WrapAround")) findOptions |= WebKit::WebFindOptionsWrapAround; else if (JSStringIsEqualToUTF8CString(optionName.get(), "StartInSelection")) findOptions |= WebKit::WebFindOptionsStartInSelection; } return DumpRenderTreeSupportGtk::findString(webView, targetString.get(), findOptions); }
static void gumjs_proxy_get_property_names (JSContextRef ctx, JSObjectRef object, JSPropertyNameAccumulatorRef property_names) { GumJscProxy * self; GumJscCore * core; self = GUMJS_PROXY (object); if (self->enumerate == NULL) return; core = JSObjectGetPrivate (JSContextGetGlobalObject (ctx)); { GumJscScope scope = GUM_JSC_SCOPE_INIT (core); JSValueRef * ex = &scope.exception; JSValueRef value; JSObjectRef names; guint length, i; value = JSObjectCallAsFunction (ctx, self->enumerate, self->receiver, 0, NULL, ex); if (value == NULL) goto beach; if (!JSValueIsArray (ctx, value)) goto invalid_result_type; names = (JSObjectRef) value; if (!_gumjs_object_try_get_uint (ctx, names, "length", &length, ex)) goto beach; for (i = 0; i != length; i++) { JSValueRef element; JSStringRef s; element = JSObjectGetPropertyAtIndex (ctx, names, i, ex); if (element == NULL) goto beach; if (!JSValueIsString (ctx, element)) goto invalid_element_type; s = JSValueToStringCopy (ctx, element, ex); if (s == NULL) goto beach; JSPropertyNameAccumulatorAddName (property_names, s); JSStringRelease (s); } goto beach; invalid_result_type: { _gumjs_throw (ctx, ex, "expected enumerate() to return an array"); goto beach; } invalid_element_type: { _gumjs_throw (ctx, ex, "expected a string"); goto beach; } beach: { _gum_jsc_scope_flush (&scope); } } }
JSValueRef JSCDocument::evaluateCustomCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef object, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { struct JSCDocumentPrivate* privData = (struct JSCDocumentPrivate*)JSObjectGetPrivate(object); if (!privData->dom || !privData->dom->xpath) return JSValueMakeUndefined(ctx); if (argumentCount < 1) { std::string errorMsg = "Wrong number of arguments in evaluate"; JSStringRef string = JSStringCreateWithUTF8CString(errorMsg.c_str()); JSValueRef exceptionString = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSValueToObject(ctx, exceptionString, NULL); return JSValueMakeUndefined(ctx); } // make sure first argument is a string if (!JSValueIsString(ctx, arguments[0])) { std::string errorMsg = "Expected xpath expression as first argument"; JSStringRef string = JSStringCreateWithUTF8CString(errorMsg.c_str()); JSValueRef exceptionString = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSValueToObject(ctx, exceptionString, NULL); return JSValueMakeUndefined(ctx); } JSStringRef stringReflocalXPath = JSValueToStringCopy(ctx, arguments[0], NULL); size_t localXPathMaxSize = JSStringGetMaximumUTF8CStringSize(stringReflocalXPath); char* localXPathBuffer = new char[localXPathMaxSize]; JSStringGetUTF8CString(stringReflocalXPath, localXPathBuffer, localXPathMaxSize); std::string localXPath(localXPathBuffer); JSStringRelease(stringReflocalXPath); free(localXPathBuffer); JSClassRef arbaicaRetClass = JSCXPathResult::getTmpl(); XPath::XPathValue<std::string>* retVal; try { if (argumentCount > 1) { // make sure second argument is a node if (!JSValueIsObject(ctx, arguments[1])) { std::string errorMsg = "Second argument is not of type node"; JSStringRef string = JSStringCreateWithUTF8CString(errorMsg.c_str()); JSValueRef exceptionString = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSValueToObject(ctx, exceptionString, NULL); return JSValueMakeUndefined(ctx); } Arabica::DOM::Node<std::string>* localContextNode = (Arabica::DOM::Node<std::string>*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[1], NULL)); retVal = new XPath::XPathValue<std::string>(privData->dom->xpath->evaluate(localXPath, *localContextNode)); } else { retVal = new XPath::XPathValue<std::string>(privData->dom->xpath->evaluate(localXPath, *privData->nativeObj)); } } catch (std::runtime_error e) { std::cout << e.what() << std::endl; return JSValueMakeUndefined(ctx); } struct JSCXPathResult::JSCXPathResultPrivate* retPrivData = new JSCXPathResult::JSCXPathResultPrivate(); retPrivData->dom = privData->dom; retPrivData->nativeObj = retVal; JSObjectRef arbaicaRetObj = JSObjectMake(ctx, arbaicaRetClass, retPrivData); return arbaicaRetObj; #if 0 if (args.Length() < 1) throw V8Exception("Wrong number of arguments in evaluate"); // if (!((V8Node::hasInstance(args[1])) && (V8XPathResult::hasInstance(args[3])))) // throw V8Exception("Parameter mismatch while calling evaluate"); v8::Local<v8::Object> self = args.Holder(); V8DocumentPrivate* privData = V8DOM::toClassPtr<V8DocumentPrivate >(self->GetInternalField(0)); v8::String::AsciiValue localExpression(args[0]); XPath::XPathValue<std::string>* retVal; if (args.Length() > 1) { Arabica::DOM::Node<std::string>* localContextNode = V8DOM::toClassPtr<Arabica::DOM::Node<std::string> >(args[1]->ToObject()->GetInternalField(0)); retVal = new XPath::XPathValue<std::string>(privData->dom->xpath->evaluate(*localExpression, *localContextNode)); } else { retVal = new XPath::XPathValue<std::string>(privData->dom->xpath->evaluate(*localExpression, *privData->nativeObj)); } v8::Handle<v8::Function> retCtor = V8XPathResult::getTmpl()->GetFunction(); v8::Persistent<v8::Object> retObj = v8::Persistent<v8::Object>::New(retCtor->NewInstance()); V8XPathResult::V8XPathResultPrivate* retPrivData = new V8XPathResult::V8XPathResultPrivate(); retPrivData->dom = privData->dom; retPrivData->nativeObj = retVal; retObj->SetInternalField(0, V8DOM::toExternal(retPrivData)); retObj.MakeWeak(0, V8XPathResult::jsDestructor); return retObj; #endif }
static void get_javascript_result( GObject *object, GAsyncResult *result, gpointer user_data) { g_printf("Here? Surely?\n"); WebKitJavascriptResult *js_result; JSValueRef value; JSGlobalContextRef context; GError *error = NULL; js_result = webkit_web_view_run_javascript_finish (WEBKIT_WEB_VIEW (object), result, &error); if (!js_result) { g_warning ("Error running javascript: %s", error->message); g_error_free (error); return; } g_printf("I'm here innit 1\n"); context = webkit_javascript_result_get_global_context (js_result); g_printf("I'm here innit 2\n"); value = webkit_javascript_result_get_value (js_result); int value_type = JSValueGetType(context, value); switch (value_type) { case kJSTypeUndefined: g_printf("Value undefined!\n"); break; case kJSTypeNull: g_printf("Value null!\n"); break; case kJSTypeBoolean: g_printf("Value boolean!\n"); break; case kJSTypeNumber: g_printf("Value number!\n"); break; case kJSTypeString: g_printf("Value string!\n"); break; case kJSTypeObject: g_printf("Value object!\n"); break; } if (JSValueIsString (context, value)) { g_printf("I'm here innit 3\n"); JSStringRef js_str_value = JSValueToStringCopy(context, value, NULL); gsize str_length = JSStringGetMaximumUTF8CStringSize(js_str_value); gchar *str_value; g_printf("String length: %d\n", str_length); str_value = (gchar *)g_malloc (str_length); JSStringGetUTF8CString (js_str_value, str_value, str_length); JSStringRelease (js_str_value); g_print ("Script result: %s\n", str_value); g_free (str_value); } else { g_warning ("Error running javascript: unexpected return value"); } webkit_javascript_result_unref (js_result); }
bool jsc::Value::isString() const {return JSValueIsString(context, value);}