Changeset View
Changeset View
Standalone View
Standalone View
source/simulation2/scripting/EngineScriptConversions.cpp
/* Copyright (C) 2017 Wildfire Games. | /* Copyright (C) 2018 Wildfire Games. | ||||
* This file is part of 0 A.D. | * This file is part of 0 A.D. | ||||
* | * | ||||
* 0 A.D. is free software: you can redistribute it and/or modify | * 0 A.D. is free software: you can redistribute it and/or modify | ||||
* it under the terms of the GNU General Public License as published by | * it under the terms of the GNU General Public License as published by | ||||
* the Free Software Foundation, either version 2 of the License, or | * the Free Software Foundation, either version 2 of the License, or | ||||
* (at your option) any later version. | * (at your option) any later version. | ||||
* | * | ||||
* 0 A.D. is distributed in the hope that it will be useful, | * 0 A.D. is distributed in the hope that it will be useful, | ||||
▲ Show 20 Lines • Show All 241 Lines • ▼ Show 20 Lines | |||||
{ | { | ||||
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(u8)); | u32 nbytes = (u32)(length * sizeof(u8)); | ||||
JS::RootedObject objArr(cx, JS_NewUint8Array(cx, length)); | JS::RootedObject objArr(cx, JS_NewUint8Array(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; | ||||
memcpy((void*)JS_GetUint8ArrayData(objArr, nogc), val.m_Data, nbytes); | bool sharedMemory; | ||||
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); | JS::RootedValue w(cx); | ||||
JS::RootedValue h(cx); | JS::RootedValue h(cx); | ||||
ScriptInterface::ToJSVal(cx, &w, val.m_W); | ScriptInterface::ToJSVal(cx, &w, val.m_W); | ||||
ScriptInterface::ToJSVal(cx, &h, val.m_H); | ScriptInterface::ToJSVal(cx, &h, val.m_H); | ||||
Show All 9 Lines | |||||
{ | { | ||||
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; | ||||
memcpy((void*)JS_GetUint16ArrayData(objArr, nogc), val.m_Data, nbytes); | bool sharedMemory; | ||||
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 w(cx); | ||||
JS::RootedValue h(cx); | JS::RootedValue h(cx); | ||||
ScriptInterface::ToJSVal(cx, &w, val.m_W); | ScriptInterface::ToJSVal(cx, &w, val.m_W); | ||||
ScriptInterface::ToJSVal(cx, &h, val.m_H); | ScriptInterface::ToJSVal(cx, &h, val.m_H); | ||||
JS::RootedObject obj(cx, JS_NewPlainObject(cx)); | JS::RootedObject obj(cx, JS_NewPlainObject(cx)); | ||||
JS_SetProperty(cx, obj, "width", w); | JS_SetProperty(cx, obj, "width", w); | ||||
JS_SetProperty(cx, obj, "height", h); | JS_SetProperty(cx, obj, "height", h); | ||||
JS_SetProperty(cx, obj, "data", data); | JS_SetProperty(cx, obj, "data", data); | ||||
ret.setObject(*obj); | ret.setObject(*obj); | ||||
} | } | ||||
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); | ||||
JS::RootedObject obj(cx, &v.toObject()); | JS::RootedObject obj(cx, &v.toObject()); | ||||
if (!JS_IsArrayObject(cx, obj)) | bool isArray; | ||||
if (!JS_IsArrayObject(cx, obj, &isArray)) | |||||
FAIL("Argument must be an array"); | FAIL("Argument must be an array"); | ||||
u32 numberOfNodes = 0; | u32 numberOfNodes = 0; | ||||
if (!JS_GetArrayLength(cx, obj, &numberOfNodes)) | if (!JS_GetArrayLength(cx, obj, &numberOfNodes)) | ||||
FAIL("Failed to get array length"); | FAIL("Failed to get array length"); | ||||
for (u32 i = 0; i < numberOfNodes; ++i) | for (u32 i = 0; i < numberOfNodes; ++i) | ||||
{ | { | ||||
▲ Show 20 Lines • Show All 61 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator