Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/EngineScriptConversions.cpp
Show First 20 Lines • Show All 108 Lines • ▼ Show 20 Lines | template<> bool ScriptInterface::FromJSVal<CColor>(JSContext* cx, JS::HandleValue v, CColor& out) | ||||
if (!JS_GetProperty(cx, obj, "a", &a) || !FromJSVal(cx, a, out.a)) | if (!JS_GetProperty(cx, obj, "a", &a) || !FromJSVal(cx, a, out.a)) | ||||
FAIL("Failed to get property CColor.a"); | FAIL("Failed to get property CColor.a"); | ||||
return true; | return true; | ||||
} | } | ||||
template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::MutableHandleValue ret, CColor const& val) | template<> void ScriptInterface::ToJSVal<CColor>(JSContext* cx, JS::MutableHandleValue ret, CColor const& val) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | CreateObject( | ||||
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); | cx, | ||||
if (!obj) | ret, | ||||
{ | "r", val.r, | ||||
ret.setUndefined(); | "g", val.g, | ||||
return; | "b", val.b, | ||||
} | "a", val.a); | ||||
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<> bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, JS::HandleValue v, fixed& out) | template<> bool ScriptInterface::FromJSVal<fixed>(JSContext* cx, JS::HandleValue v, fixed& out) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
double ret; | double ret; | ||||
if (!JS::ToNumber(cx, v, &ret)) | if (!JS::ToNumber(cx, v, &ret)) | ||||
return false; | return false; | ||||
▲ Show 20 Lines • Show All 93 Lines • ▼ Show 20 Lines | template<> void ScriptInterface::ToJSVal<Grid<u8> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u8>& val) | ||||
// Copy the array data and then remove the no-GC check to allow further changes to the JS data | // Copy the array data and then remove the no-GC check to allow further changes to the JS data | ||||
{ | { | ||||
JS::AutoCheckCannotGC nogc; | JS::AutoCheckCannotGC nogc; | ||||
bool sharedMemory; | bool sharedMemory; | ||||
memcpy((void*)JS_GetUint8ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes); | memcpy((void*)JS_GetUint8ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes); | ||||
} | } | ||||
JS::RootedValue data(cx, JS::ObjectValue(*objArr)); | JS::RootedValue data(cx, JS::ObjectValue(*objArr)); | ||||
JS::RootedValue w(cx); | CreateObject( | ||||
JS::RootedValue h(cx); | cx, | ||||
ScriptInterface::ToJSVal(cx, &w, val.m_W); | ret, | ||||
ScriptInterface::ToJSVal(cx, &h, val.m_H); | "width", val.m_W, | ||||
"height", val.m_H, | |||||
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); | "data", data); | ||||
JS_SetProperty(cx, obj, "width", w); | |||||
JS_SetProperty(cx, obj, "height", h); | |||||
JS_SetProperty(cx, obj, "data", data); | |||||
ret.setObject(*obj); | |||||
} | } | ||||
template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u16>& val) | template<> void ScriptInterface::ToJSVal<Grid<u16> >(JSContext* cx, JS::MutableHandleValue ret, const Grid<u16>& val) | ||||
{ | { | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
u32 length = (u32)(val.m_W * val.m_H); | u32 length = (u32)(val.m_W * val.m_H); | ||||
u32 nbytes = (u32)(length * sizeof(u16)); | u32 nbytes = (u32)(length * sizeof(u16)); | ||||
JS::RootedObject objArr(cx, JS_NewUint16Array(cx, length)); | JS::RootedObject objArr(cx, JS_NewUint16Array(cx, length)); | ||||
// Copy the array data and then remove the no-GC check to allow further changes to the JS data | // Copy the array data and then remove the no-GC check to allow further changes to the JS data | ||||
{ | { | ||||
JS::AutoCheckCannotGC nogc; | JS::AutoCheckCannotGC nogc; | ||||
bool sharedMemory; | bool sharedMemory; | ||||
memcpy((void*)JS_GetUint16ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes); | memcpy((void*)JS_GetUint16ArrayData(objArr, &sharedMemory, nogc), val.m_Data, nbytes); | ||||
} | } | ||||
JS::RootedValue data(cx, JS::ObjectValue(*objArr)); | 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_NewPlainObject(cx)); | |||||
JS_SetProperty(cx, obj, "width", w); | |||||
JS_SetProperty(cx, obj, "height", h); | |||||
JS_SetProperty(cx, obj, "data", data); | |||||
ret.setObject(*obj); | CreateObject( | ||||
cx, | |||||
ret, | |||||
"width", val.m_W, | |||||
"height", val.m_H, | |||||
"data", data); | |||||
} | } | ||||
template<> bool ScriptInterface::FromJSVal<TNSpline>(JSContext* cx, JS::HandleValue v, TNSpline& out) | template<> bool ScriptInterface::FromJSVal<TNSpline>(JSContext* cx, JS::HandleValue v, TNSpline& out) | ||||
{ | { | ||||
if (!v.isObject()) | if (!v.isObject()) | ||||
FAIL("Argument must be an object"); | FAIL("Argument must be an object"); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
▲ Show 20 Lines • Show All 75 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator