bool rs::jsapi::Object::Set(JSContext* cx, JS::HandleObject obj, JS::HandleId id, bool strict, JS::MutableHandleValue vp) { JSAutoRequest ar(cx); auto state = Object::GetState(cx, obj); if (state != nullptr && state->setter != nullptr) { Value value(cx, vp); char nameBuffer[256]; auto name = JSID_TO_STRING(id); auto nameLength = JS_EncodeStringToBuffer(cx, name, nameBuffer, sizeof(nameBuffer) - 1); try { if (nameLength < sizeof(nameBuffer)) { nameBuffer[nameLength] = '\0'; state->setter(nameBuffer, value); } else { std::vector<char> nameVector(nameLength + 1); nameLength = JS_EncodeStringToBuffer(cx, name, &nameVector[0], nameVector.size() - 1); nameVector[nameLength] = '\0'; state->setter(&nameVector[0], value); } return true; } catch (const std::exception& ex) { JS_ReportError(cx, ex.what()); return false; } } else { // TODO: what will this do to the JS? vp.setUndefined(); 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(); }
void CNetClient::GuiPoll(JS::MutableHandleValue ret) { if (m_GuiMessageQueue.empty()) { ret.setUndefined(); return; } ret.set(m_GuiMessageQueue.front()); m_GuiMessageQueue.pop_front(); }
template<typename T> static void ToJSVal_vector(JSContext* cx, JS::MutableHandleValue ret, const std::vector<T>& val) { JSAutoRequest rq(cx); JS::RootedObject obj(cx, JS_NewArrayObject(cx, 0)); if (!obj) { ret.setUndefined(); return; } for (u32 i = 0; i < val.size(); ++i) { JS::RootedValue el(cx); ScriptInterface::ToJSVal<T>(cx, &el, val[i]); JS_SetElement(cx, obj, i, el); } ret.setObject(*obj); }
// TODO: This is copy-pasted from scriptinterface/ScriptConversions.cpp (#define VECTOR stuff), would be nice to remove the duplication template<> void ScriptInterface::ToJSVal<std::vector<CFixedVector2D> >(JSContext* cx, JS::MutableHandleValue ret, const std::vector<CFixedVector2D>& val) { JSAutoRequest rq(cx); JS::RootedObject obj(cx, JS_NewArrayObject(cx, 0)); if (!obj) { ret.setUndefined(); return; } for (size_t i = 0; i < val.size(); ++i) { JS::RootedValue el(cx); ScriptInterface::ToJSVal<CFixedVector2D>(cx, &el, val[i]); JS_SetElement(cx, obj, i, el); } ret.setObject(*obj); }
template<> void ScriptInterface::ToJSVal<CFixedVector2D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector2D& val) { JSAutoRequest rq(cx); // apply the Vector2D prototype to the return value ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); JS::RootedObject proto(cx, &pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR2DPROTO).toObject()); JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, proto, JS::NullPtr())); if (!obj) { ret.setUndefined(); return; } JS::RootedValue x(cx); JS::RootedValue y(cx); ToJSVal(cx, &x, val.X); ToJSVal(cx, &y, val.Y); JS_SetProperty(cx, obj, "x", x); JS_SetProperty(cx, obj, "y", y); ret.setObject(*obj); }
NS_IMETHODIMP nsScriptErrorBase::GetStack(JS::MutableHandleValue aStack) { aStack.setUndefined(); return NS_OK; }