Showing the changes to libraries/script-engine/src/v8/FastScriptValueUtils.cpp (1 of 4 files). Show all
Unified · Split
| @@ -17,9 +17,11 @@ · 16 unmodified lines | |||
| 17 | #include "V8Types.h" | 17 | #include "V8Types.h" |
| 18 | #include "ScriptValueV8Wrapper.h" | 18 | #include "ScriptValueV8Wrapper.h" |
| 19 | 19 | ||
| 20 | ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { | 20 | #ifdef CONVERSIONS_OPTIMIZED_FOR_V8 |
| 21 | auto prototype = engine->globalObject().property("__hifi_vec3__"); | 21 | |
| 22 | if (!prototype.hasProperty("defined") || !prototype.property("defined").toBool()) { | 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()) { | ||
| 23 | prototype = engine->evaluate( | 25 | prototype = engine->evaluate( |
| 24 | "__hifi_vec3__ = Object.defineProperties({}, { " | 26 | "__hifi_vec3__ = Object.defineProperties({}, { " |
| 25 | "defined: { value: true }," | 27 | "defined: { value: true }," |
| @@ -33,7 +35,7 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 7 unmodified lines | |||
| 33 | "green: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } }," | 35 | "green: { set: function(nv) { return this.y = nv; }, get: function() { return this.y; } }," |
| 34 | "blue: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } }" | 36 | "blue: { set: function(nv) { return this.z = nv; }, get: function() { return this.z; } }" |
| 35 | "})"); | 37 | "})"); |
| 36 | } | 38 | }*/ |
| 37 | ScriptValue value = engine->newObject(); | 39 | ScriptValue value = engine->newObject(); |
| 38 | 40 | ||
| 39 | ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(value); | 41 | ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(value); |
| @@ -49,6 +51,51 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 9 unmodified lines | |||
| 49 | V8ScriptValue v8ScriptValue = proxy->toV8Value(); | 51 | V8ScriptValue v8ScriptValue = proxy->toV8Value(); |
| 50 | v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::Cast(v8ScriptValue.get()); | 52 | v8::Local<v8::Object> v8Object = v8::Local<v8::Object>::Cast(v8ScriptValue.get()); |
| 51 | 53 | ||
| 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 | |||
| 52 | if (!v8Object->Set(context, v8::String::NewFromUtf8(isolate, "x").ToLocalChecked(), v8::Number::New(isolate, vec3.x)).FromMaybe(false)) { | 99 | if (!v8Object->Set(context, v8::String::NewFromUtf8(isolate, "x").ToLocalChecked(), v8::Number::New(isolate, vec3.x)).FromMaybe(false)) { |
| 53 | Q_ASSERT(false); | 100 | Q_ASSERT(false); |
| 54 | } | 101 | } |
| @@ -59,15 +106,15 @@ ScriptValue vec3ToScriptValueFast(ScriptEngine* engine, const glm::vec3& vec3) { · 4 unmodified lines | |||
| 59 | Q_ASSERT(false); | 106 | Q_ASSERT(false); |
| 60 | } | 107 | } |
| 61 | 108 | ||
| 62 | auto v8Prototype = ScriptValueV8Wrapper::fullUnwrap(engineV8, prototype); | 109 | //auto v8Prototype = ScriptValueV8Wrapper::fullUnwrap(engineV8, prototype); |
| 63 | if (!v8Object->SetPrototype(context, v8Prototype.get()).FromMaybe(false)) { | 110 | if (!v8Object->SetPrototype(context, prototype).FromMaybe(false)) { |
| 64 | Q_ASSERT(false); | 111 | Q_ASSERT(false); |
| 65 | } | 112 | } |
| 66 | value.setPrototype(prototype); | 113 | //value.setPrototype(prototype); |
| 67 | return value; | 114 | return value; |
| 68 | } | 115 | } |
| 69 | 116 | ||
| 70 | bool vec3FromScriptValueFast(const ScriptValue& object, glm::vec3& vec3) { | 117 | bool vec3FromScriptValue(const ScriptValue& object, glm::vec3& vec3) { |
| 71 | ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(object); | 118 | ScriptValueV8Wrapper *proxy = ScriptValueV8Wrapper::unwrap(object); |
| 72 | 119 | ||
| 73 | auto engineV8 = proxy->getV8Engine(); | 120 | auto engineV8 = proxy->getV8Engine(); |
| @@ -185,3 +232,5 @@ bool vec3FromScriptValueFast(const ScriptValue& object, glm::vec3& vec3) { · 111 unmodified lines | |||
| 185 | } | 232 | } |
| 186 | return true; | 233 | return true; |
| 187 | } | 234 | } |
| 235 | |||
| 236 | #endif | ||