History

vec3ToScriptValue optimizations

ksuprynowicz committed 3 years ago

Commit 8b9cd841 parent 43939294 files · +77 −47Browse files

Showing the changes to libraries/script-engine/src/v8/FastScriptValueUtils.cpp (1 of 4 files). Show all

Unified · Split

libraries/script-engine/src/v8/FastScriptValueUtils.cpp+57−8
@@ -17,9 +17,11 @@ · 16 unmodified lines
1717 #include "V8Types.h"
1818 #include "ScriptValueV8Wrapper.h"
1919
20−ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) {
21− auto prototype = engine->globalObject().property("__hifi_vec3__");
22− if (!prototype.hasProperty("defined") || !prototype.property("defined").toBool()) {
20+#ifdef CONVERSIONS_OPTIMIZED_FOR_V8
21+
22+ScriptValue vec3ToScriptValue(ScriptEngine* engine, const glm::vec3& vec3) {
23+ //auto prototype = engine->globalObject().property("__hifi_vec3__");
24+ /*if (!prototype.hasProperty("defined") || !prototype.property("defined").toBool()) {
2325 prototype = engine->evaluate(
2426 "__hifi_vec3__ = Object.defineProperties({}, { "
2527 "defined: { value: true },"
@@ -33,7 +35,7 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 7 unmodified lines
3335 "green: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } },"
3436 "blue: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } }"
3537 "})");
36− }
38+ }*/
3739 ScriptValue value = engine->newObject();
3840
3941 ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(value);
@@ -49,6 +51,51 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 9 unmodified lines
4951 V8ScriptValue v8ScriptValue = proxy->toV8Value();
5052 v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::Cast(v8ScriptValue.get());
5153
54+ v8::Local<v8::Value> prototype;
55+ bool hasPrototype = false;
56+
57+ if (context->Global()->Get(context, v8::String::NewFromUtf8(isolate, "__hifi_vec3__").ToLocalChecked()).ToLocal(&prototype)) {
58+ if (!prototype->IsNullOrUndefined() && prototype->IsObject()) {
59+ v8::Local<v8::Object> prototypeObject = v8::Local<v8::Object>::Cast(prototype);
60+ v8::Local<v8::Value> isDefined;
61+ if (prototypeObject->Get(context, v8::String::NewFromUtf8(isolate, "defined").ToLocalChecked()).ToLocal(&isDefined)) {
62+ if ((!isDefined->IsNullOrUndefined()) && isDefined->BooleanValue(isolate)) {
63+ hasPrototype = true;
64+ }
65+ }
66+ }
67+ }
68+
69+ if (!hasPrototype) {
70+ QString sourceCode("globalThis.__hifi_vec3__ = Object.defineProperties({}, { "
71+ "defined: { value: true },"
72+ "0: { set: function(nv) { return this.x = nv; }, get: function() { return this.x; } },"
73+ "1: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } },"
74+ "2: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } },"
75+ "r: { set: function(nv) { return this.x = nv; }, get: function() { return this.x; } },"
76+ "g: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } },"
77+ "b: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } },"
78+ "red: { set: function(nv) { return this.x = nv; }, get: function() { return this.x; } },"
79+ "green: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } },"
80+ "blue: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } }"
81+ "})");
82+ v8::TryCatch tryCatch(isolate);
83+ v8::ScriptOrigin scriptOrigin(isolate, v8::String::NewFromUtf8(isolate, "Vec3prototype").ToLocalChecked());
84+ v8::Local<v8::Script> script;
85+ if (!v8::Script::Compile(context, v8::String::NewFromUtf8(isolate, sourceCode.toStdString().c_str()).ToLocalChecked(), &scriptOrigin).ToLocal(&script)) {
86+ Q_ASSERT(false);
87+ }
88+ v8::Local<v8::Value> result;
89+ if (!script->Run(context).ToLocal(&result)) {
90+ Q_ASSERT(false);
91+ }
92+ if (!context->Global()->Get(context, v8::String::NewFromUtf8(isolate, "__hifi_vec3__").ToLocalChecked()).ToLocal(&prototype)) {
93+ Q_ASSERT(false);
94+ }
95+ Q_ASSERT(!tryCatch.HasCaught());
96+ qDebug() <<"vec3ToScriptValue: creating prototype";
97+ }
98+
5299 if (!v8Object->Set(context, v8::String::NewFromUtf8(isolate, "x").ToLocalChecked(), v8::Number::New(isolate, vec3.x)).FromMaybe(false)) {
53100 Q_ASSERT(false);
54101 }
@@ -59,15 +106,15 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 4 unmodified lines
59106 Q_ASSERT(false);
60107 }
61108
62− auto v8Prototype = ScriptValueV8Wrapper::fullUnwrap(engineV8, prototype);
63− if (!v8Object->SetPrototype(context, v8Prototype.get()).FromMaybe(false)) {
109+ //auto v8Prototype = ScriptValueV8Wrapper::fullUnwrap(engineV8, prototype);
110+ if (!v8Object->SetPrototype(context, prototype).FromMaybe(false)) {
64111 Q_ASSERT(false);
65112 }
66− value.setPrototype(prototype);
113+ //value.setPrototype(prototype);
67114 return value;
68115 }
69116
70−bool vec3FromScriptValueFast(const ScriptValue& object, glm::vec3& vec3) {
117+bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3) {
71118 ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(object);
72119
73120 auto engineV8 = proxy->getV8Engine();
@@ -185,3 +232,5 @@ bool vec3FromScriptValueFast(const ScriptValue& object, glm::vec3& vec3) { · 111 unmodified lines
185232 }
186233 return true;
187234 }
235+
236+#endif