void JSListenerBase::setJSDelegate(JSContext *cx, JS::HandleValue func) { if (nullptr != proFunc) { delete proFunc; proFunc = nullptr; } proFunc = new JS::PersistentRootedObject(cx, func.toObjectOrNull()); }
bool jsval_to_physics3DRigidBodyDes(JSContext* cx, JS::HandleValue v, Physics3DRigidBodyDes* des) { JS::RootedObject jsobj(cx, v.toObjectOrNull()); JS::RootedValue tmp(cx); if(JS_GetProperty(cx, jsobj, "mass", &tmp)) { des->mass = tmp.toNumber(); } if(JS_GetProperty(cx, jsobj, "shape", &tmp)) { JS::RootedObject tmpObj(cx, tmp.toObjectOrNull()); js_proxy_t* proxy = jsb_get_js_proxy(tmpObj); des->shape = proxy ? (cocos2d::Physics3DShape*)proxy->ptr : nullptr; } if(JS_GetProperty(cx, jsobj, "localInertia", &tmp)) { Vec3 v3; jsval_to_vector3(cx, tmp, &v3); des->localInertia = v3; } if(JS_GetProperty(cx, jsobj, "originalTransform", &tmp)) { Mat4 m4; jsval_to_matrix(cx, tmp, &m4); des->originalTransform = m4; } if(JS_GetProperty(cx, jsobj, "disableSleep", &tmp)) { des->disableSleep = tmp.toBoolean(); } return true; }
bool JavaScriptShared::toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to) { switch (JS_TypeOfValue(cx, from)) { case JSTYPE_VOID: *to = UndefinedVariant(); return true; case JSTYPE_OBJECT: case JSTYPE_FUNCTION: { RootedObject obj(cx, from.toObjectOrNull()); if (!obj) { MOZ_ASSERT(from == JSVAL_NULL); *to = NullVariant(); return true; } if (xpc_JSObjectIsID(cx, obj)) { JSIID iid; const nsID *id = xpc_JSObjectToID(cx, obj); ConvertID(*id, &iid); *to = iid; return true; } ObjectVariant objVar; if (!toObjectVariant(cx, obj, &objVar)) return false; *to = objVar; return true; } case JSTYPE_STRING: { nsAutoJSString autoStr; if (!autoStr.init(cx, from)) return false; *to = autoStr; return true; } case JSTYPE_NUMBER: if (from.isInt32()) *to = double(from.toInt32()); else *to = from.toDouble(); return true; case JSTYPE_BOOLEAN: *to = from.toBoolean(); return true; default: MOZ_ASSERT(false); return false; } }
void setJSCallback(JS::HandleValue jsFunc, JS::HandleObject jsTarget) { if (_callback != nullptr) { CC_SAFE_DELETE(_callback); } JSContext* cx = ScriptingCore::getInstance()->getGlobalContext(); _callback = new (std::nothrow) JSFunctionWrapper(cx, jsTarget, jsFunc); _jsFunc = jsFunc.toObjectOrNull(); }
bool jsval_to_TerrainData(JSContext* cx, JS::HandleValue v, Terrain::TerrainData* ret) { JS::RootedObject jsobj(cx, v.toObjectOrNull()); JS::RootedValue js_heightMap(cx); JS::RootedValue js_alphaMap(cx); JS::RootedValue js_chunkSize(cx); JS::RootedValue js_mapHeight(cx); JS::RootedValue js_mapScale(cx); JS::RootedValue js_detailMap(cx); std::string heightMap, alphaMap; Size chunkSize; double mapScale, mapHeight; bool ok = true; ok &= JS_GetProperty(cx, jsobj, "heightMap", &js_heightMap) && JS_GetProperty(cx, jsobj, "alphaMap", &js_alphaMap) && JS_GetProperty(cx, jsobj, "chunkSize", &js_chunkSize) && JS_GetProperty(cx, jsobj, "mapHeight", &js_mapHeight) && JS_GetProperty(cx, jsobj, "mapScale", &js_mapScale) && JS_GetProperty(cx, jsobj, "detailMap", &js_detailMap) && jsval_to_std_string(cx, js_heightMap, &heightMap) && jsval_to_std_string(cx, js_alphaMap, &alphaMap) && jsval_to_ccsize(cx, js_chunkSize, &chunkSize) && JS::ToNumber(cx, js_mapScale, &mapScale) && JS::ToNumber(cx, js_mapHeight, &mapHeight); JSB_PRECONDITION2(ok, cx, false, "Error processing arguments"); ret->_heightMapSrc = heightMap.c_str(); char* tmp = (char*)malloc(sizeof(char) * (alphaMap.size() + 1)); strcpy(tmp, alphaMap.c_str()); tmp[alphaMap.size()] = '\0'; ret->_alphaMapSrc = tmp; ret->_chunkSize = chunkSize; ret->_mapHeight = mapHeight; ret->_mapScale = mapScale; ret->_skirtHeightRatio = 1; JS::RootedObject jsobj_detailMap(cx, js_detailMap.toObjectOrNull()); uint32_t length = 0; JS_GetArrayLength(cx, jsobj_detailMap, &length); for(uint32_t i = 0; i < length; ++i) { JS::RootedValue element(cx); JS_GetElement(cx, jsobj_detailMap, i, &element); Terrain::DetailMap dm; jsval_to_DetailMap(cx, element, &dm); ret->_detailMaps[i] = dm; } ret->_detailMapAmount = length; return true; }
std::vector<Vec3> jsval_to_std_vector_vec3(JSContext* cx, JS::HandleValue v) { std::vector<Vec3> ret; JS::RootedObject jsobj(cx, v.toObjectOrNull()); uint32_t length; JS_GetArrayLength(cx, jsobj, &length); ret.reserve(length); for(size_t i = 0; i < length; ++i) { JS::RootedValue element(cx); JS_GetElement(cx, jsobj, (uint32_t)i, &element); Vec3 v3; jsval_to_vector3(cx, element, &v3); ret.push_back(v3); } return ret; }
void JSAudioNodeThreaded::set(JS::HandleValue val) { if (!val.isObject()) return; JS::RootedObject global(m_Cx, JSAudioContext::GetContext()->m_JsGlobalObj); JS::RootedObject thisObj(m_Cx, this->getJSObject()); JS::RootedObject props(m_Cx, val.toObjectOrNull()); JS::Rooted<JS::IdVector> ida(m_Cx, JS::IdVector(m_Cx)); JS_Enumerate(m_Cx, props, &ida); for (size_t i = 0; i < ida.length(); i++) { // Retrieve the current key & value JS::RootedId id(m_Cx, ida[i]); JSAutoByteString key(m_Cx, JSID_TO_STRING(id)); JS::RootedValue val(m_Cx); if (!JS_GetPropertyById(m_Cx, props, id, &val)) { break; } // Set the property JS::RootedObject hashObj(m_Cx, m_HashObj); JS_SetProperty(m_Cx, hashObj, key.ptr(), val); // Call the setter callback JSTransferableFunction *fun = m_Node->getFunction(JSAudioNodeCustomBase::SETTER_FN); if (fun->isSet()) { JS::RootedValue rval(m_Cx); JS::RootedString jskey(m_Cx, JS_NewStringCopyZ(m_Cx, key.ptr())); JS::AutoValueArray<3> params(m_Cx); params[0].setString(jskey); params[1].set(val); params[2].setObjectOrNull(global); fun->call(thisObj, params, &rval); } } }
bool jsval_to_DetailMap(JSContext* cx, JS::HandleValue v, Terrain::DetailMap* ret) { JS::RootedObject jsobj(cx, v.toObjectOrNull()); JS::RootedValue js_file(cx); JS::RootedValue js_size(cx); std::string file; double size; bool ok = JS_GetProperty(cx, jsobj, "file", &js_file) && JS_GetProperty(cx, jsobj, "size", &js_size) && jsval_to_std_string(cx, js_file, &file) && JS::ToNumber(cx, js_size, &size); JSB_PRECONDITION2(ok, cx, false, "Error processing arguments"); ret->_detailMapSrc = file; ret->_detailMapSize = size; return true; }
OID OIDInfo::getOID(JSContext* cx, JS::HandleValue value) { JS::RootedObject obj(cx, value.toObjectOrNull()); return getOID(cx, obj); }
bool JavaScriptShared::toVariant(JSContext *cx, JS::HandleValue from, JSVariant *to) { switch (JS_TypeOfValue(cx, from)) { case JSTYPE_VOID: *to = void_t(); return true; case JSTYPE_NULL: { *to = uint64_t(0); return true; } case JSTYPE_OBJECT: case JSTYPE_FUNCTION: { RootedObject obj(cx, from.toObjectOrNull()); if (!obj) { MOZ_ASSERT(from == JSVAL_NULL); *to = uint64_t(0); return true; } if (xpc_JSObjectIsID(cx, obj)) { JSIID iid; const nsID *id = xpc_JSObjectToID(cx, obj); ConvertID(*id, &iid); *to = iid; return true; } ObjectId id; if (!makeId(cx, obj, &id)) return false; *to = uint64_t(id); return true; } case JSTYPE_STRING: { nsDependentJSString dep; if (!dep.init(cx, from)) return false; *to = dep; return true; } case JSTYPE_NUMBER: if (JSVAL_IS_INT(from)) *to = double(from.toInt32()); else *to = from.toDouble(); return true; case JSTYPE_BOOLEAN: *to = from.toBoolean(); return true; default: MOZ_ASSERT(false); return false; } }
Status MongoStatusInfo::toStatus(JSContext* cx, JS::HandleValue value) { return *static_cast<Status*>(JS_GetPrivate(value.toObjectOrNull())); }
Decimal128 NumberDecimalInfo::ToNumberDecimal(JSContext* cx, JS::HandleValue thisv) { auto x = static_cast<Decimal128*>(JS_GetPrivate(thisv.toObjectOrNull())); return x ? *x : Decimal128(0); }
int NumberIntInfo::ToNumberInt(JSContext* cx, JS::HandleValue thisv) { auto x = static_cast<int*>(JS_GetPrivate(thisv.toObjectOrNull())); return x ? *x : 0; }
ObjectWrapper::ObjectWrapper(JSContext* cx, JS::HandleValue value) : _context(cx), _object(cx, value.toObjectOrNull()) {}