template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::MutableHandleValue ret, CColor const& val) { JSAutoRequest rq(cx); JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); if (!obj) { ret.setUndefined(); return; } JS::RootedValue r(cx); JS::RootedValue g(cx); JS::RootedValue b(cx); JS::RootedValue a(cx); ToJSVal(cx, &r, val.r); ToJSVal(cx, &g, val.g); ToJSVal(cx, &b, val.b); ToJSVal(cx, &a, val.a); JS_SetProperty(cx, obj, "r", r); JS_SetProperty(cx, obj, "g", g); JS_SetProperty(cx, obj, "b", b); JS_SetProperty(cx, obj, "a", a); ret.setObject(*obj); }
template<> void ScriptInterface::ToJSVal<IComponent*>(JSContext* cx, JS::MutableHandleValue ret, IComponent* const& val) { JSAutoRequest rq(cx); if (val == NULL) { ret.setNull(); return; } // If this is a scripted component, just return the JS object directly JS::RootedValue instance(cx, val->GetJSInstance()); if (!instance.isNull()) { ret.set(instance); return; } // Otherwise we need to construct a wrapper object // (TODO: cache wrapper objects?) JS::RootedObject obj(cx); if (!val->NewJSObject(*ScriptInterface::GetScriptInterfaceAndCBData(cx)->pScriptInterface, &obj)) { // Report as an error, since scripts really shouldn't try to use unscriptable interfaces LOGERROR("IComponent does not have a scriptable interface"); ret.setUndefined(); return; } JS_SetPrivate(obj, static_cast<void*>(val)); ret.setObject(*obj); }
template<> void ScriptInterface::ToJSVal<CFixedVector3D>(JSContext* cx, JS::MutableHandleValue ret, const CFixedVector3D& val) { JSAutoRequest rq(cx); // apply the Vector3D prototype to the return value; ScriptInterface::CxPrivate* pCxPrivate = ScriptInterface::GetScriptInterfaceAndCBData(cx); JS::RootedObject proto(cx, &pCxPrivate->pScriptInterface->GetCachedValue(ScriptInterface::CACHE_VECTOR3DPROTO).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); JS::RootedValue z(cx); ToJSVal(cx, &x, val.X); ToJSVal(cx, &y, val.Y); ToJSVal(cx, &z, val.Z); JS_SetProperty(cx, obj, "x", x); JS_SetProperty(cx, obj, "y", y); JS_SetProperty(cx, obj, "z", z); ret.setObject(*obj); }
bool gjs_lookup_interface_constructor(JSContext *context, GType gtype, JS::MutableHandleValue value_p) { JSObject *constructor; GIBaseInfo *interface_info; interface_info = g_irepository_find_by_gtype(NULL, gtype); if (interface_info == NULL) { gjs_throw(context, "Cannot expose non introspectable interface %s", g_type_name(gtype)); return false; } g_assert(g_base_info_get_type(interface_info) == GI_INFO_TYPE_INTERFACE); constructor = gjs_lookup_generic_constructor(context, interface_info); if (G_UNLIKELY (constructor == NULL)) return false; g_base_info_unref(interface_info); value_p.setObject(*constructor); return true; }
/*! @brief Convert a YARP list into a %JavaScript object. @param[in] jct The %JavaScript engine context. @param[in,out] theData The output object. @param[in] inputValue The value to be processed. */ static void convertList(JSContext * jct, JS::MutableHandleValue theData, const yarp::os::Bottle & inputValue) { ODL_ENTER(); //#### ODL_P2("jct = ", jct, "inputValue = ", &inputValue); //#### JSObject * valueArray = JS_NewArrayObject(jct, 0); if (valueArray) { JS::RootedObject arrayRooted(jct); JS::RootedValue anElement(jct); JS::RootedId aRootedId(jct); arrayRooted = valueArray; for (int ii = 0, mm = inputValue.size(); mm > ii; ++ii) { yarp::os::Value aValue(inputValue.get(ii)); convertValue(jct, &anElement, aValue); if (JS_IndexToId(jct, ii, &aRootedId)) { JS_SetPropertyById(jct, arrayRooted, aRootedId, anElement); } } theData.setObject(*valueArray); } ODL_EXIT(); //#### } // convertList
NS_IMETHODIMP nsUDPMessage::GetRawData(JSContext* cx, JS::MutableHandleValue aRawData) { if(!mJsobj){ mJsobj = dom::Uint8Array::Create(cx, nullptr, mData.Length(), mData.Elements()); HoldJSObjects(this); } aRawData.setObject(*mJsobj); return NS_OK; }
static bool surface_from_g_argument(JSContext *context, JS::MutableHandleValue value_p, GIArgument *arg) { JSObject *obj; obj = gjs_cairo_surface_from_surface(context, (cairo_surface_t*)arg->v_pointer); if (!obj) return false; value_p.setObject(*obj); return true; }
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<Grid<u16> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u16>& val) { JSAutoRequest rq(cx); u32 length = (u32)(val.m_W * val.m_H); u32 nbytes = (u32)(length * sizeof(u16)); JS::RootedObject objArr(cx, JS_NewUint16Array(cx, length)); memcpy((void*)JS_GetUint16ArrayData(objArr), val.m_Data, nbytes); JS::RootedValue data(cx, JS::ObjectValue(*objArr)); JS::RootedValue w(cx); JS::RootedValue h(cx); ScriptInterface::ToJSVal(cx, &w, val.m_W); ScriptInterface::ToJSVal(cx, &h, val.m_H); JS::RootedObject obj(cx, JS_NewObject(cx, nullptr, JS::NullPtr(), JS::NullPtr())); JS_SetProperty(cx, obj, "width", w); JS_SetProperty(cx, obj, "height", h); JS_SetProperty(cx, obj, "data", data); ret.setObject(*obj); }
/*! @brief Convert a YARP dictionary into a %JavaScript object. @param[in] jct The %JavaScript engine context. @param[in,out] theData The output object. @param[in] inputAsList The input dictionary as a list. */ static void convertDictionary(JSContext * jct, JS::MutableHandleValue theData, const yarp::os::Bottle & inputAsList) { ODL_ENTER(); //#### ODL_P3("jct = ", jct, "theData = ", &theData, "inputAsList = ", &inputAsList); //#### JS::RootedObject empty(jct); JSObject * valueObject = JS_NewObject(jct, NULL); if (valueObject) { JS::RootedObject objectRooted(jct); JS::RootedValue anElement(jct); objectRooted = valueObject; for (int ii = 0, mm = inputAsList.size(); mm > ii; ++ii) { yarp::os::Value anEntry(inputAsList.get(ii)); if (anEntry.isList()) { yarp::os::Bottle * entryAsList = anEntry.asList(); if (entryAsList && (2 == entryAsList->size())) { yarp::os::Value aValue(entryAsList->get(1)); convertValue(jct, &anElement, aValue); JS_SetProperty(jct, objectRooted, entryAsList->get(0).toString().c_str(), anElement); } } } theData.setObject(*valueObject); } ODL_EXIT(); //#### } // convertDictionary
void WebGL2Context::GetActiveUniforms(JSContext* cx, const WebGLProgram& program, const dom::Sequence<GLuint>& uniformIndices, GLenum pname, JS::MutableHandleValue retval) { const char funcName[] = "getActiveUniforms"; retval.setNull(); if (IsContextLost()) return; if (!ValidateUniformEnum(this, pname, funcName)) return; if (!ValidateObject("getActiveUniforms: program", program)) return; const auto& numActiveUniforms = program.LinkInfo()->uniforms.size(); for (const auto& curIndex : uniformIndices) { if (curIndex >= numActiveUniforms) { ErrorInvalidValue("%s: Too-large active uniform index queried.", funcName); return; } } const auto& count = uniformIndices.Length(); JS::Rooted<JSObject*> array(cx, JS_NewArrayObject(cx, count)); UniquePtr<GLint[]> samples(new GLint[count]); if (!array || !samples) { ErrorOutOfMemory("%s: Failed to allocate buffers.", funcName); return; } retval.setObject(*array); MakeContextCurrent(); gl->fGetActiveUniformsiv(program.mGLName, count, uniformIndices.Elements(), pname, samples.get()); switch (pname) { case LOCAL_GL_UNIFORM_TYPE: case LOCAL_GL_UNIFORM_SIZE: case LOCAL_GL_UNIFORM_BLOCK_INDEX: case LOCAL_GL_UNIFORM_OFFSET: case LOCAL_GL_UNIFORM_ARRAY_STRIDE: case LOCAL_GL_UNIFORM_MATRIX_STRIDE: for (size_t i = 0; i < count; ++i) { JS::RootedValue value(cx); value = JS::Int32Value(samples[i]); if (!JS_DefineElement(cx, array, i, value, JSPROP_ENUMERATE)) return; } break; case LOCAL_GL_UNIFORM_IS_ROW_MAJOR: for (size_t i = 0; i < count; ++i) { JS::RootedValue value(cx); value = JS::BooleanValue(samples[i]); if (!JS_DefineElement(cx, array, i, value, JSPROP_ENUMERATE)) return; } break; default: MOZ_CRASH("Invalid pname"); } }