Skip to content

Commit

Permalink
Merge pull request #868 from overte-org/feature/script_byte_array
Browse files Browse the repository at this point in the history
Add qByteArray to script value conversion
  • Loading branch information
ksuprynowicz authored Apr 16, 2024
2 parents f1475e4 + c50a4f9 commit 46a80fc
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -125,3 +125,11 @@ void ScriptManagerScriptingInterface::removeServerEntityScriptMessagesRequest(co
_manager->engine()->raiseException("Uuid must not be specified when removeServerEntityScriptMessagesRequest is invoked from entity script");
}
}

QString ScriptManagerScriptingInterface::btoa(const QByteArray &binary) {
return binary.toBase64();
}

QByteArray ScriptManagerScriptingInterface::atob(const QString &base64) {
return QByteArray::fromBase64(base64.toUtf8());
}
16 changes: 16 additions & 0 deletions libraries/script-engine/src/ScriptManagerScriptingInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,22 @@ class ScriptManagerScriptingInterface : public QObject {
Q_INVOKABLE void removeServerEntityScriptMessagesRequest();
Q_INVOKABLE void removeServerEntityScriptMessagesRequest(const QUuid& entityID);

/*@jsdoc
* This decodes Base64 string and returns contents as ArrayBuffer.
* @function Script.atob
* @param {String} base64 - String with Base64-encoded binary data.
* @returns {ArrayBuffer} Decoded binary data.
*/
Q_INVOKABLE QByteArray atob(const QString &base64);

/*@jsdoc
* This encodes ArrayBuffer and returns Base64-encoded string.
* @function Script.btoa
* @param {ArrayBuffer} binary - Data to be encoded.
* @returns {String} String with Base64-encoded binary data.
*/
Q_INVOKABLE QString btoa(const QByteArray &binary);

signals:

/*@jsdoc
Expand Down
1 change: 1 addition & 0 deletions libraries/script-engine/src/ScriptValueUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ void registerMetaTypes(ScriptEngine* engine) {
scriptRegisterMetaType<QUrl, qURLToScriptValue, qURLFromScriptValue>(engine);
scriptRegisterMetaType<QColor, qColorToScriptValue, qColorFromScriptValue>(engine);
scriptRegisterMetaType<QTimer*, qTimerToScriptValue, qTimerFromScriptValue>(engine, "QTimer*");
scriptRegisterMetaType<QByteArray, qBytearrayToScriptValue, qBytearrayFromScriptValue>(engine, "QByteArray");

scriptRegisterMetaType<PickRay, pickRayToScriptValue, pickRayFromScriptValue>(engine);
scriptRegisterMetaType<Collision, collisionToScriptValue, collisionFromScriptValue>(engine);
Expand Down
45 changes: 45 additions & 0 deletions libraries/script-engine/src/v8/FastScriptValueUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,48 @@

#ifdef CONVERSIONS_OPTIMIZED_FOR_V8

ScriptValue qBytearrayToScriptValue(ScriptEngine* engine, const QByteArray &qByteArray) {
auto engineV8 = dynamic_cast<ScriptEngineV8*>(engine);
Q_ASSERT(engineV8);
auto isolate = engineV8->getIsolate();
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
auto context = engineV8->getContext();
v8::Context::Scope contextScope(context);
v8::Local<v8::ArrayBuffer> arrayBuffer = v8::ArrayBuffer::New(isolate, qByteArray.size());
memcpy(arrayBuffer->GetBackingStore()->Data(), qByteArray.data(), qByteArray.size());
v8::Local<v8::Value> arrayBufferValue = v8::Local<v8::Value>::Cast(arrayBuffer);

return {new ScriptValueV8Wrapper(engineV8, V8ScriptValue(engineV8, arrayBufferValue))};
}

bool qBytearrayFromScriptValue(const ScriptValue& object, QByteArray &qByteArray) {
ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(object);
if (!proxy) {
return false;
}

auto engineV8 = proxy->getV8Engine();

auto isolate = engineV8->getIsolate();
v8::Locker locker(isolate);
v8::Isolate::Scope isolateScope(isolate);
v8::HandleScope handleScope(isolate);
auto context = engineV8->getContext();
v8::Context::Scope contextScope(context);
V8ScriptValue v8ScriptValue = proxy->toV8Value();

v8::Local<v8::Value> v8Value = v8ScriptValue.get();
if(!v8Value->IsArrayBuffer()) {
return false;
}
v8::Local<v8::ArrayBuffer> arrayBuffer = v8::Local<v8::ArrayBuffer>::Cast(v8Value);
qByteArray.resize(arrayBuffer->ByteLength());
memcpy(qByteArray.data(), arrayBuffer->Data(), arrayBuffer->ByteLength());
return true;
}

ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3) {
ScriptValue value = engine->newObject();

Expand Down Expand Up @@ -98,6 +140,9 @@ ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3) {

bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3) {
ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(object);
if (!proxy) {
return false;
}

auto engineV8 = proxy->getV8Engine();

Expand Down
4 changes: 4 additions & 0 deletions libraries/script-engine/src/v8/FastScriptValueUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
#define CONVERSIONS_OPTIMIZED_FOR_V8

#ifdef CONVERSIONS_OPTIMIZED_FOR_V8
ScriptValue qBytearrayToScriptValue(ScriptEngine* engine, const QByteArray &qByteArray);

bool qBytearrayFromScriptValue(const ScriptValue& object, QByteArray &qByteArray);

ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3);

bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3);
Expand Down

0 comments on commit 46a80fc

Please sign in to comment.