JSValueRef JSCCharacterData::replaceDataCallback(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 == 3 && JSValueIsNumber(ctx, arguments[0]) && JSValueIsNumber(ctx, arguments[1]) && JSValueIsString(ctx, arguments[2])) { unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); unsigned long localCount = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); JSStringRef stringReflocalArg = JSValueToStringCopy(ctx, arguments[2], 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->replaceData(localOffset, localCount, localArg); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling replaceData"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
static JSValueRef getResourceDocumentNode(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* /*exception*/) { JSValueRef undefined = JSValueMakeUndefined(ctx); InspectorController* controller = reinterpret_cast<InspectorController*>(JSObjectGetPrivate(thisObject)); if (!argumentCount || argumentCount > 1 || !controller) return undefined; JSValueRef identifierValue = arguments[0]; if (!JSValueIsNumber(ctx, identifierValue)) return undefined; unsigned long identifier = static_cast<unsigned long>(JSValueToNumber(ctx, identifierValue, 0)); RefPtr<InspectorResource> resource = controller->resources().get(identifier); ASSERT(resource); if (!resource) return undefined; FrameLoader* frameLoader = resource->loader->frameLoader(); if (!frameLoader) return undefined; Document* document = frameLoader->frame()->document(); if (!document) return undefined; if (document->isPluginDocument() || document->isImageDocument()) return undefined; KJS::JSLock lock; JSValueRef documentValue = toRef(toJS(toJS(controller->scriptContext()), document)); return documentValue; }
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); }
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); }
JSObjectRef MakeInstance (JSContextRef ctx, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { size_t length = 1; if (argumentCount > 0) { *exception = nullptr; if (!JSValueIsNumber(ctx,arguments[0])) { JSStringRef string = JSStringCreateWithUTF8CString("required first argument to be a number which is the size of the buffer requested"); JSValueRef message = JSValueMakeString(ctx, string); JSStringRelease(string); *exception = JSObjectMakeError(ctx, 1, &message, 0); return nullptr; } length = JSValueToNumber(ctx, arguments[0], exception); CHECK_BUF_EXCEPTION(nullptr); } JSBuffer *buffer = (JSBuffer *)malloc(sizeof(JSBuffer)); buffer->buffer = malloc(length); buffer->length = length; buffer->type = JSBufferTypePointer; memset(buffer->buffer, 0, length); // by default, let's set the value to NAN float *p = (float*)buffer->buffer; p[0] = NAN; JSObjectRef object = MakeObjectForJSBuffer(ctx,buffer); return object; }
JSObjectRef JSCArrayBuffer::jsConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { uscxml::ArrayBuffer* localInstance = NULL; if (false) { } else if (argumentCount == 1 && JSValueIsNumber(ctx, arguments[0])) { unsigned long localLength = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); localInstance = new uscxml::ArrayBuffer(localLength); } if (!localInstance) { JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling constructor for ArrayBuffer"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return (JSObjectRef)JSValueMakeNull(ctx); } JSClassRef retClass = JSCArrayBuffer::getTmpl(); struct JSCArrayBuffer::JSCArrayBufferPrivate* retPrivData = new JSCArrayBuffer::JSCArrayBufferPrivate(); retPrivData->nativeObj = localInstance; JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData); return retObj; }
unsigned JSArray::length() { JSValueRef val = JSObjectGetProperty(ctx_, instance_, JSString("length"), nullptr); if (JSValueIsNumber(ctx_, val)) return static_cast<unsigned>(JSValueToNumber(ctx_, val, nullptr)); return 0; }
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; }
JSValueRef function_file_output_stream_write(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argc, const JSValueRef args[], JSValueRef *exception) { if (argc == 2 && JSValueGetType(ctx, args[0]) == kJSTypeString && JSValueGetType(ctx, args[1]) == kJSTypeObject) { char *descriptor = value_to_c_string(ctx, args[0]); unsigned int count = (unsigned int) array_get_count(ctx, (JSObjectRef) args[1]); uint8_t buf[count]; for (unsigned int i = 0; i < count; i++) { JSValueRef v = array_get_value_at_index(ctx, (JSObjectRef) args[1], i); if (JSValueIsNumber(ctx, v)) { double n = JSValueToNumber(ctx, v, NULL); if (0 <= n && n <= 255) { buf[i] = (uint8_t) n; } else { fprintf(stderr, "Output stream value out of range %f", n); } } else { fprintf(stderr, "Output stream value not a number"); } } file_write(descriptor_str_to_int(descriptor), count, buf); free(descriptor); } return JSValueMakeNull(ctx); }
JSValueRef JSCArrayBuffer::sliceCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { struct JSCArrayBufferPrivate* privData = (struct JSCArrayBufferPrivate*)JSObjectGetPrivate(thisObj); if (false) { } else if (argumentCount == 2 && JSValueIsNumber(ctx, arguments[0]) && JSValueIsNumber(ctx, arguments[1])) { long localBegin = (long)JSValueToNumber(ctx, arguments[0], exception); long localEnd = (long)JSValueToNumber(ctx, arguments[1], exception); uscxml::ArrayBuffer* retVal = new uscxml::ArrayBuffer(privData->nativeObj->slice(localBegin, localEnd)); JSClassRef retClass = JSCArrayBuffer::getTmpl(); struct JSCArrayBuffer::JSCArrayBufferPrivate* retPrivData = new JSCArrayBuffer::JSCArrayBufferPrivate(); retPrivData->dom = privData->dom; retPrivData->nativeObj = retVal; JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData); return retObj; } else if (argumentCount == 1 && JSValueIsNumber(ctx, arguments[0])) { long localBegin = (long)JSValueToNumber(ctx, arguments[0], exception); uscxml::ArrayBuffer* retVal = new uscxml::ArrayBuffer(privData->nativeObj->slice(localBegin)); JSClassRef retClass = JSCArrayBuffer::getTmpl(); struct JSCArrayBuffer::JSCArrayBufferPrivate* retPrivData = new JSCArrayBuffer::JSCArrayBufferPrivate(); retPrivData->dom = privData->dom; retPrivData->nativeObj = retVal; JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData); return retObj; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling slice"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
double WebViewTest::javascriptResultToNumber(WebKitJavascriptResult* javascriptResult) { JSGlobalContextRef context = webkit_javascript_result_get_global_context(javascriptResult); g_assert(context); JSValueRef value = webkit_javascript_result_get_value(javascriptResult); g_assert(value); g_assert(JSValueIsNumber(context, value)); return JSValueToNumber(context, value, 0); }
JSValueRef JSCCharacterData::deleteDataCallback(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 == 2 && JSValueIsNumber(ctx, arguments[0]) && JSValueIsNumber(ctx, arguments[1])) { unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); unsigned long localCount = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); privData->nativeObj->deleteData(localOffset, localCount); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling deleteData"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
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 bool console_setSharedDouble(JSContextRef ctx, JSObjectRef thisObject, JSStringRef /*propertyName*/, JSValueRef value, JSValueRef* exception) { if (!JSValueIsNumber(ctx, value)) return false; CallJSDlg* dlg = static_cast<CallJSDlg*>(JSObjectGetPrivate(thisObject)); if (!dlg) return false; double temp = JSValueToNumber (ctx, value, exception); if (exception && *exception) return false; dlg->setSharedDouble(temp); return true; }
JSValueRef JSCInt16Array::getCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { struct JSCInt16ArrayPrivate* privData = (struct JSCInt16ArrayPrivate*)JSObjectGetPrivate(thisObj); if (false) { } else if (argumentCount == 1 && JSValueIsNumber(ctx, arguments[0])) { unsigned long localIndex = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); short retVal = privData->nativeObj->get(localIndex); JSValueRef jscRetVal = JSValueMakeNumber(ctx, retVal); return jscRetVal; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling get"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
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); }
gboolean js_to_dbus(JSContextRef ctx, const JSValueRef jsvalue, DBusMessageIter *iter, const char* sig, JSValueRef* exception) { DBusSignatureIter s_iter; dbus_signature_iter_init(&s_iter, sig); int type; switch (type = dbus_signature_iter_get_current_type(&s_iter)) { case DBUS_TYPE_BOOLEAN: { dbus_bool_t value = JSValueToBoolean(ctx, jsvalue); if (!dbus_message_iter_append_basic(iter, type, (void*)&value)) { g_warning("signatuer:%c error!", type); return FALSE; } else { return TRUE; } } case DBUS_TYPE_DOUBLE: CASE_NUMBER { if (!JSValueIsNumber(ctx, jsvalue)) { js_fill_exception(ctx, exception, "jsvalue is not an number!"); return FALSE; } double value = JSValueToNumber(ctx, jsvalue, NULL); if (!dbus_message_iter_append_basic(iter, type, (void*)&value)) { g_warning("signatuer:%c error!", type); return FALSE; } else { return TRUE; } } CASE_STRING { char* value = jsvalue_to_cstr(ctx, jsvalue); if (value == NULL || !dbus_message_iter_append_basic(iter, type, (void*)&value)) { g_free(value); js_fill_exception(ctx, exception, "jsvalue is not an string or memory not enough!"); return FALSE; } else { g_free(value); return TRUE; } } case DBUS_TYPE_STRUCT: { if (!jsvalue_instanceof(ctx, jsvalue, "Array")) { js_fill_exception(ctx, exception, "jsvalue should an array"); return FALSE; } JSPropertyNameArrayRef prop_names = JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue); int p_num = JSPropertyNameArrayGetCount(prop_names); if (p_num == 0) { JSPropertyNameArrayRelease(prop_names); js_fill_exception(ctx, exception, "Struct at least have one element!"); return FALSE; } DBusMessageIter sub_iter; OPEN_CONTAINER(iter, type, NULL, &sub_iter); DBusSignatureIter sub_s_iter; dbus_signature_iter_recurse(&s_iter, &sub_s_iter); for (int i=0; i<p_num; i++) { JSValueRef value = JSObjectGetProperty(ctx, (JSObjectRef)jsvalue, JSPropertyNameArrayGetNameAtIndex(prop_names, i), NULL); char *sig = dbus_signature_iter_get_signature(&sub_s_iter); if (!js_to_dbus(ctx, value, &sub_iter, sig, exception)) { js_fill_exception(ctx, exception, "Failed append struct with sig:%sTODO"); dbus_free(sig); return FALSE; } dbus_free(sig); if (i != p_num-1 && !dbus_signature_iter_next(&sub_s_iter)) { JSPropertyNameArrayRelease(prop_names); CLOSE_CONTAINER(iter, &sub_iter); js_fill_exception(ctx, exception, "to many params filled to struct"); return FALSE; } } if (dbus_signature_iter_next(&sub_s_iter)) { JSPropertyNameArrayRelease(prop_names); CLOSE_CONTAINER(iter, &sub_iter); js_fill_exception(ctx, exception, "need more params by this struct"); return FALSE; } JSPropertyNameArrayRelease(prop_names); CLOSE_CONTAINER(iter, &sub_iter); return TRUE; } case DBUS_TYPE_ARRAY: if (dbus_signature_iter_get_element_type(&s_iter) == DBUS_TYPE_DICT_ENTRY) { DBusSignatureIter dict_s_iter; dbus_signature_iter_recurse(&s_iter, &dict_s_iter); char *d_sig = dbus_signature_iter_get_signature(&dict_s_iter); DBusMessageIter sub_iter; OPEN_CONTAINER(iter, type, d_sig, &sub_iter); dbus_free(d_sig); JSPropertyNameArrayRef prop_names = JSObjectCopyPropertyNames(ctx, (JSObjectRef)jsvalue); int p_num = JSPropertyNameArrayGetCount(prop_names); DBusSignatureIter dict_sub_s_iter; dbus_signature_iter_recurse(&dict_s_iter, &dict_sub_s_iter); int key_type = dbus_signature_iter_get_current_type(&dict_sub_s_iter); dbus_signature_iter_next(&dict_sub_s_iter); char *val_sig = dbus_signature_iter_get_signature(&dict_sub_s_iter); for (int i=0; i<p_num; i++) { DBusMessageIter dict_iter; OPEN_CONTAINER(&sub_iter, DBUS_TYPE_DICT_ENTRY, NULL, &dict_iter); JSStringRef key_str = JSPropertyNameArrayGetNameAtIndex(prop_names, i); //TODO: fetch key type switch (key_type) { CASE_STRING { char *value = jsstring_to_cstr(ctx, key_str); dbus_message_iter_append_basic(&dict_iter, key_type, (void*)&value); g_free(value); break; } case DBUS_TYPE_DOUBLE: CASE_NUMBER { //TODO detect illegal number format JSValueRef excp; double value = JSValueToNumber(ctx, JSValueMakeString(ctx, key_str), &excp); if (excp != NULL) { js_fill_exception(ctx, exception, "dict_entry's key must be an number to match the signature!"); return FALSE; } dbus_message_iter_append_basic(&dict_iter, key_type, (void*)&value); break; } default: { js_fill_exception(ctx, exception, "DICT_ENTRY's key must basic type, and you should not see this warning in javascript runtime"); dbus_free(val_sig); JSPropertyNameArrayRelease(prop_names); CLOSE_CONTAINER(iter, &sub_iter); return FALSE; } } js_to_dbus(ctx, JSObjectGetProperty(ctx, (JSObjectRef)jsvalue, key_str, NULL), &dict_iter, val_sig, exception); CLOSE_CONTAINER(&sub_iter, &dict_iter); } dbus_free(val_sig); JSPropertyNameArrayRelease(prop_names); CLOSE_CONTAINER(iter, &sub_iter); return TRUE; } else {
JSValueRef JSCInt16Array::setCallback(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObj, size_t argumentCount, const JSValueRef* arguments, JSValueRef* exception) { struct JSCInt16ArrayPrivate* privData = (struct JSCInt16ArrayPrivate*)JSObjectGetPrivate(thisObj); if (false) { } else if (argumentCount == 2 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCInt16Array::getTmpl()) && JSValueIsNumber(ctx, arguments[1])) { uscxml::Int16Array* localArray = ((struct JSCInt16Array::JSCInt16ArrayPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); privData->nativeObj->set(localArray, localOffset); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } else if (argumentCount == 2 && JSValueIsNumber(ctx, arguments[0]) && JSValueIsNumber(ctx, arguments[1])) { unsigned long localIndex = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); short localValue = (short)JSValueToNumber(ctx, arguments[1], exception); privData->nativeObj->set(localIndex, localValue); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } else if (argumentCount == 2 && JSValueIsObject(ctx, arguments[0]) && JSValueIsNumber(ctx, arguments[1])) { std::vector<short> localArray; JSValueRef localArrayItem; unsigned int localArrayIndex = 0; while((localArrayItem = JSObjectGetPropertyAtIndex(ctx, JSValueToObject(ctx, arguments[0], exception), localArrayIndex, exception))) { if (JSValueIsUndefined(ctx, localArrayItem)) break; if (JSValueIsNumber(ctx,localArrayItem)) localArray.push_back(JSValueToNumber(ctx, localArrayItem, exception)); localArrayIndex++; } unsigned long localOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); privData->nativeObj->set(localArray, localOffset); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } else if (argumentCount == 1 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCInt16Array::getTmpl())) { uscxml::Int16Array* localArray = ((struct JSCInt16Array::JSCInt16ArrayPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; privData->nativeObj->set(localArray); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } else if (argumentCount == 1 && JSValueIsObject(ctx, arguments[0])) { std::vector<short> localArray; JSValueRef localArrayItem; unsigned int localArrayIndex = 0; while((localArrayItem = JSObjectGetPropertyAtIndex(ctx, JSValueToObject(ctx, arguments[0], exception), localArrayIndex, exception))) { if (JSValueIsUndefined(ctx, localArrayItem)) break; if (JSValueIsNumber(ctx,localArrayItem)) localArray.push_back(JSValueToNumber(ctx, localArrayItem, exception)); localArrayIndex++; } privData->nativeObj->set(localArray); JSValueRef jscRetVal = JSValueMakeUndefined(ctx); return jscRetVal; } JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling set"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return JSValueMakeUndefined(ctx); }
JSValueRef function_http_request(JSContextRef ctx, JSObjectRef function, JSObjectRef this_object, size_t argc, const JSValueRef args[], JSValueRef* exception) { if (argc == 1 && JSValueGetType(ctx, args[0]) == kJSTypeObject) { JSObjectRef opts = JSValueToObject(ctx, args[0], NULL); JSValueRef url_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("url"), NULL); char *url = value_to_c_string(ctx, url_ref); JSValueRef timeout_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("timeout"), NULL); time_t timeout = 0; if (JSValueIsNumber(ctx, timeout_ref)) { timeout = (time_t) JSValueToNumber(ctx, timeout_ref, NULL); } JSValueRef method_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("method"), NULL); char *method = value_to_c_string(ctx, method_ref); JSValueRef body_ref = JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("body"), NULL); JSObjectRef headers_obj = JSValueToObject(ctx, JSObjectGetProperty(ctx, opts, JSStringCreateWithUTF8CString("headers"), NULL), NULL); CURL *handle = curl_easy_init(); assert(handle != NULL); curl_easy_setopt(handle, CURLOPT_CUSTOMREQUEST, method); curl_easy_setopt(handle, CURLOPT_URL, url); struct curl_slist *headers = NULL; if (!JSValueIsNull(ctx, headers_obj)) { JSPropertyNameArrayRef properties = JSObjectCopyPropertyNames(ctx, headers_obj); size_t n = JSPropertyNameArrayGetCount(properties); for (int i = 0; i < n; i++) { JSStringRef key_str = JSPropertyNameArrayGetNameAtIndex(properties, i); JSValueRef val_ref = JSObjectGetProperty(ctx, headers_obj, key_str, NULL); size_t len = JSStringGetLength(key_str) + 1; char *key = malloc(len * sizeof(char)); JSStringGetUTF8CString(key_str, key, len); JSStringRef val_as_str = to_string(ctx, val_ref); char *val = value_to_c_string(ctx, JSValueMakeString(ctx, val_as_str)); JSStringRelease(val_as_str); size_t len_key = strlen(key); size_t len_val = strlen(val); char *header = malloc((len_key + len_val + 2 + 1) * sizeof(char)); sprintf(header, "%s: %s", key, val); curl_slist_append(headers, header); free(header); free(key); free(val); } curl_easy_setopt(handle, CURLOPT_HEADER, headers); } // curl_easy_setopt(handle, CURLOPT_HEADER, 1L); curl_easy_setopt(handle, CURLOPT_TIMEOUT, timeout); struct read_string_state input_state; if (!JSValueIsUndefined(ctx, body_ref)) { char *body = value_to_c_string(ctx, body_ref); input_state.input = body; input_state.offset = 0; input_state.length = strlen(body); curl_easy_setopt(handle, CURLOPT_READDATA, &input_state); curl_easy_setopt(handle, CURLOPT_READFUNCTION, read_string_callback); } JSObjectRef response_headers = JSObjectMake(ctx, NULL, NULL); struct header_state header_state; header_state.ctx = ctx; header_state.headers = response_headers; curl_easy_setopt(handle, CURLOPT_HEADERDATA, &header_state); curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, header_to_object_callback); struct write_state body_state; body_state.offset = 0; body_state.length = 0; body_state.data = NULL; curl_easy_setopt(handle, CURLOPT_WRITEDATA, &body_state); curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, write_string_callback); JSObjectRef result = JSObjectMake(ctx, NULL, NULL); int res = curl_easy_perform(handle); if (res != 0) { JSStringRef error_str = JSStringCreateWithUTF8CString(curl_easy_strerror(res)); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("error"), JSValueMakeString(ctx, error_str), kJSPropertyAttributeReadOnly, NULL); } int status = 0; curl_easy_getinfo(handle, CURLINFO_RESPONSE_CODE, &status); // printf("%d bytes, %x\n", body_state.offset, body_state.data); if (body_state.data != NULL) { JSStringRef body_str = JSStringCreateWithUTF8CString(body_state.data); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("body"), JSValueMakeString(ctx, body_str), kJSPropertyAttributeReadOnly, NULL); free(body_state.data); } JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("status"), JSValueMakeNumber(ctx, status), kJSPropertyAttributeReadOnly, NULL); JSObjectSetProperty(ctx, result, JSStringCreateWithUTF8CString("headers"), response_headers, kJSPropertyAttributeReadOnly, NULL); curl_slist_free_all(headers); curl_easy_cleanup(handle); return result; } return JSValueMakeNull(ctx); }
JSObjectRef JSCInt16Array::jsConstructor(JSContextRef ctx, JSObjectRef constructor, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { uscxml::Int16Array* localInstance = NULL; if (false) { } else if (argumentCount == 3 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCArrayBuffer::getTmpl()) && JSValueIsNumber(ctx, arguments[1]) && JSValueIsNumber(ctx, arguments[2])) { uscxml::ArrayBuffer* localBuffer = ((struct JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; unsigned long localByteOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); unsigned long localLength = (unsigned long)JSValueToNumber(ctx, arguments[2], exception); localInstance = new uscxml::Int16Array(localBuffer, localByteOffset, localLength); } else if (argumentCount == 2 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCArrayBuffer::getTmpl()) && JSValueIsNumber(ctx, arguments[1])) { uscxml::ArrayBuffer* localBuffer = ((struct JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; unsigned long localByteOffset = (unsigned long)JSValueToNumber(ctx, arguments[1], exception); localInstance = new uscxml::Int16Array(localBuffer, localByteOffset); } else if (argumentCount == 1 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCInt16Array::getTmpl())) { uscxml::Int16Array* localArray = ((struct JSCInt16Array::JSCInt16ArrayPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; localInstance = new uscxml::Int16Array(localArray); } else if (argumentCount == 1 && JSValueIsObject(ctx, arguments[0]) && JSValueIsObjectOfClass(ctx, arguments[0], JSCArrayBuffer::getTmpl())) { uscxml::ArrayBuffer* localBuffer = ((struct JSCArrayBuffer::JSCArrayBufferPrivate*)JSObjectGetPrivate(JSValueToObject(ctx, arguments[0], exception)))->nativeObj; localInstance = new uscxml::Int16Array(localBuffer); } else if (argumentCount == 1 && JSValueIsNumber(ctx, arguments[0])) { unsigned long localLength = (unsigned long)JSValueToNumber(ctx, arguments[0], exception); localInstance = new uscxml::Int16Array(localLength); } else if (argumentCount == 1 && JSValueIsObject(ctx, arguments[0])) { std::vector<short> localArray; JSValueRef localArrayItem; unsigned int localArrayIndex = 0; while((localArrayItem = JSObjectGetPropertyAtIndex(ctx, JSValueToObject(ctx, arguments[0], exception), localArrayIndex, exception))) { if (JSValueIsUndefined(ctx, localArrayItem)) break; if (JSValueIsNumber(ctx,localArrayItem)) localArray.push_back(JSValueToNumber(ctx, localArrayItem, exception)); localArrayIndex++; } localInstance = new uscxml::Int16Array(localArray); } if (!localInstance) { JSStringRef exceptionString = JSStringCreateWithUTF8CString("Parameter mismatch while calling constructor for Int16Array"); *exception = JSValueMakeString(ctx, exceptionString); JSStringRelease(exceptionString); return (JSObjectRef)JSValueMakeNull(ctx); } JSClassRef retClass = JSCInt16Array::getTmpl(); struct JSCInt16Array::JSCInt16ArrayPrivate* retPrivData = new JSCInt16Array::JSCInt16ArrayPrivate(); retPrivData->nativeObj = localInstance; JSObjectRef retObj = JSObjectMake(ctx, retClass, retPrivData); return retObj; }
bool jsc::Value::isNumber() const {return JSValueIsNumber(context, value);}
static JSValueRef addSourceToFrame(JSContextRef ctx, JSObjectRef /*function*/, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* /*exception*/) { JSValueRef undefined = JSValueMakeUndefined(ctx); InspectorController* controller = reinterpret_cast<InspectorController*>(JSObjectGetPrivate(thisObject)); if (argumentCount < 2 || !controller) return undefined; JSValueRef identifierValue = arguments[0]; if (!JSValueIsNumber(ctx, identifierValue)) return undefined; unsigned long identifier = static_cast<unsigned long>(JSValueToNumber(ctx, identifierValue, 0)); RefPtr<InspectorResource> resource = controller->resources().get(identifier); ASSERT(resource); if (!resource) return undefined; RefPtr<SharedBuffer> buffer; if (resource->requestURL == resource->loader->requestURL()) buffer = resource->loader->mainResourceData(); else { FrameLoader* frameLoader = resource->loader->frameLoader(); if (!frameLoader) return undefined; Document* doc = frameLoader->frame()->document(); if (!doc) return undefined; CachedResource* cachedResource = doc->docLoader()->cachedResource(resource->requestURL.url()); if (!cachedResource) return undefined; buffer = cachedResource->data(); } if (!buffer) return undefined; String textEncodingName = resource->loader->overrideEncoding(); if (!textEncodingName) textEncodingName = resource->textEncodingName; TextEncoding encoding(textEncodingName); if (!encoding.isValid()) encoding = WindowsLatin1Encoding(); String sourceString = encoding.decode(buffer->data(), buffer->size()); Node* node = toNode(toJS(arguments[1])); ASSERT(node); if (!node) return undefined; if (!node->attached()) { ASSERT_NOT_REACHED(); return undefined; } ASSERT(node->isElementNode()); if (!node->isElementNode()) return undefined; Element* element = static_cast<Element*>(node); ASSERT(element->isFrameOwnerElement()); if (!element->isFrameOwnerElement()) return undefined; HTMLFrameOwnerElement* frameOwner = static_cast<HTMLFrameOwnerElement*>(element); ASSERT(frameOwner->contentFrame()); if (!frameOwner->contentFrame()) return undefined; FrameLoader* loader = frameOwner->contentFrame()->loader(); loader->setResponseMIMEType(resource->mimeType); loader->begin(); loader->write(sourceString); loader->end(); return undefined; }