/** * gjs_string_from_ucs4: * @cx: a #JSContext * @ucs4_string: string of #gunichar * @n_chars: number of characters in @ucs4_string or -1 for zero-terminated * @value_p: JS::Value that will be filled with a string * * Returns: true on success, false otherwise in which case a JS error is thrown */ bool gjs_string_from_ucs4(JSContext *cx, const gunichar *ucs4_string, ssize_t n_chars, JS::MutableHandleValue value_p) { long u16_string_length; GError *error = NULL; char16_t *u16_string = reinterpret_cast<char16_t *>(g_ucs4_to_utf16(ucs4_string, n_chars, NULL, &u16_string_length, &error)); if (!u16_string) { gjs_throw(cx, "Failed to convert UCS-4 string to UTF-16: %s", error->message); g_error_free(error); return false; } JSAutoRequest ar(cx); /* Avoid a copy - assumes that g_malloc == js_malloc == malloc */ JS::RootedString str(cx, JS_NewUCString(cx, u16_string, u16_string_length)); if (!str) { gjs_throw(cx, "Failed to convert UCS-4 string to UTF-16"); return false; } value_p.setString(str); return true; }
bool JSGlobal::JSGetter___dirname(JSContext *cx, JS::MutableHandleValue vp) { Path path(JSUtils::CurrentJSCaller(cx), false, true); vp.setString(JS_NewStringCopyZ(cx, path.dir())); return true; }
bool JSGlobal::JSGetter___filename(JSContext *cx, JS::MutableHandleValue vp) { char *filename = JSUtils::CurrentJSCaller(cx); vp.setString(JS_NewStringCopyZ(cx, filename)); free(filename); return true; }
template<> void ScriptInterface::ToJSVal<const char*>(JSContext* cx, JS::MutableHandleValue ret, const char* const& val) { JSAutoRequest rq(cx); JS::RootedString str(cx, JS_NewStringCopyZ(cx, val)); if (str) ret.setString(str); else ret.setUndefined(); }
template<> void ScriptInterface::ToJSVal<std::string>(JSContext* cx, JS::MutableHandleValue ret, const std::string& val) { JSAutoRequest rq(cx); JS::RootedString str(cx, JS_NewStringCopyN(cx, val.c_str(), val.length())); if (str) ret.setString(str); else ret.setUndefined(); }
template<> void ScriptInterface::ToJSVal<std::wstring>(JSContext* cx, JS::MutableHandleValue ret, const std::wstring& val) { JSAutoRequest rq(cx); utf16string utf16(val.begin(), val.end()); JS::RootedString str(cx, JS_NewUCStringCopyN(cx, reinterpret_cast<const char16_t*> (utf16.c_str()), utf16.length())); if (str) ret.setString(str); else ret.setUndefined(); }
CEXPORT bool def_timestep_image_map_get_url(JSContext *cx, JS::HandleObject obj, JS::HandleId id, JS::MutableHandleValue vp) { JS_BeginRequest(cx); timestep_image_map *thiz = (timestep_image_map*)JS_GetPrivate(obj.get()); if (thiz) { vp.setString(JS_NewStringCopyZ(cx, thiz->url)); } JS_EndRequest(cx); return true; }
void IdWrapper::toValue(JS::MutableHandleValue value) const { if (isInt()) { value.setInt32(toInt32()); return; } if (isString()) { auto str = JSID_TO_STRING(_value); value.setString(str); return; } uasserted(ErrorCodes::BadValue, "Failed to toValue() non-string and non-integer jsid"); }
bool gjs_string_from_utf8_n(JSContext *cx, const char *utf8_chars, size_t len, JS::MutableHandleValue out) { JSAutoRequest ar(cx); JS::UTF8Chars chars(utf8_chars, len); JS::RootedString str(cx, JS_NewStringCopyUTF8N(cx, chars)); if (str) out.setString(str); return !!str; }
bool gjs_string_from_utf8(JSContext *context, const char *utf8_string, JS::MutableHandleValue value_p) { JS_BeginRequest(context); JS::ConstUTF8CharsZ chars(utf8_string, strlen(utf8_string)); JS::RootedString str(context, JS_NewStringCopyUTF8Z(context, chars)); if (str) value_p.setString(str); JS_EndRequest(context); return str != nullptr; }
bool gjs_string_from_utf8(JSContext *context, const char *utf8_string, ssize_t n_bytes, JS::MutableHandleValue value_p) { char16_t *u16_string; glong u16_string_length; GError *error; /* intentionally using n_bytes even though glib api suggests n_chars; with * n_chars (from g_utf8_strlen()) the result appears truncated */ error = NULL; u16_string = reinterpret_cast<char16_t *>(g_utf8_to_utf16(utf8_string, n_bytes, NULL, &u16_string_length, &error)); if (!u16_string) { gjs_throw(context, "Failed to convert UTF-8 string to " "JS string: %s", error->message); g_error_free(error); return false; } JS_BeginRequest(context); /* Avoid a copy - assumes that g_malloc == js_malloc == malloc */ JS::RootedString str(context, JS_NewUCString(context, u16_string, u16_string_length)); if (str) value_p.setString(str); JS_EndRequest(context); return str != NULL; }
static void convertValue(JSContext * jct, JS::MutableHandleValue theData, const yarp::os::Value & inputValue) { ODL_ENTER(); //#### ODL_P2("jct = ", jct, "inputValue = ", &inputValue); //#### if (inputValue.isBool()) { theData.setBoolean(inputValue.asBool()); } else if (inputValue.isInt()) { theData.setInt32(inputValue.asInt()); } else if (inputValue.isString()) { YarpString value = inputValue.asString(); JSString * aString = JS_NewStringCopyZ(jct, value.c_str()); if (aString) { theData.setString(aString); } } else if (inputValue.isDouble()) { theData.setDouble(inputValue.asDouble()); } else if (inputValue.isDict()) { yarp::os::Property * value = inputValue.asDict(); if (value) { yarp::os::Bottle asList(value->toString()); convertDictionary(jct, theData, asList); } } else if (inputValue.isList()) { yarp::os::Bottle * value = inputValue.asList(); if (value) { yarp::os::Property asDict; if (ListIsReallyDictionary(*value, asDict)) { convertDictionary(jct, theData, *value); } else { convertList(jct, theData, *value); } } } else { // We don't know what to do with this... theData.setNull(); } ODL_EXIT(); //#### } // convertValue