Showing the changes to libraries/script-engine/src/v8/ScriptValueIteratorV8Wrapper.cpp (1 of 40 files). Show all
Unified · Split
| @@ -0,0 +1,82 @@ | ||
| 1 | +// | |
| 2 | +// ScriptValueIteratorV8Wrapper.cpp | |
| 3 | +// libraries/script-engine/src/v8 | |
| 4 | +// | |
| 5 | +// Created by Heather Anderson on 8/29/21. | |
| 6 | +// Modified for V8 by dr Karol Suprynowicz on 2022/10/08 | |
| 7 | +// Copyright 2021 Vircadia contributors. | |
| 8 | +// Copyright 2022 Overte e.V. | |
| 9 | +// | |
| 10 | +// Distributed under the Apache License, Version 2.0. | |
| 11 | +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html | |
| 12 | +// | |
| 13 | + | |
| 14 | +#include "ScriptValueIteratorV8Wrapper.h" | |
| 15 | + | |
| 16 | +V8ScriptValueIterator::V8ScriptValueIterator(ScriptEngineV8* engine, v8::Local<v8::Value> object) : _engine(engine) { | |
| 17 | + _context.Reset(_engine->getIsolate(), _engine->getContext()); | |
| 18 | + auto context = _context.Get(_engine->getIsolate()); | |
| 19 | + v8::Local<v8::Object> v8Object; | |
| 20 | + if (!object->ToObject(context).ToLocal(&v8Object)) { | |
| 21 | + Q_ASSERT(false); | |
| 22 | + } | |
| 23 | + _object.Reset(_engine->getIsolate(), v8Object); | |
| 24 | + _propertyNames.Reset(_engine->getIsolate(), v8Object->GetOwnPropertyNames(context).ToLocalChecked()); | |
| 25 | + _length = _propertyNames.Get(_engine->getIsolate())->Length(); | |
| 26 | + _currentIndex = 0; | |
| 27 | +} | |
| 28 | + | |
| 29 | +bool V8ScriptValueIterator::hasNext() const { | |
| 30 | + return _currentIndex < _length - 1; | |
| 31 | +} | |
| 32 | + | |
| 33 | +QString V8ScriptValueIterator::name() const { | |
| 34 | + auto context = _context.Get(_engine->getIsolate()); | |
| 35 | + v8::Local<v8::Value> propertyName; | |
| 36 | + if (!_propertyNames.Get(_engine->getIsolate())->Get(context, _length).ToLocal(&propertyName)) { | |
| 37 | + Q_ASSERT(false); | |
| 38 | + } | |
| 39 | + return QString(*v8::String::Utf8Value(_engine->getIsolate(), propertyName)); | |
| 40 | +} | |
| 41 | + | |
| 42 | +void V8ScriptValueIterator::next() { | |
| 43 | + if (_length < _currentIndex - 1) { | |
| 44 | + _length++; | |
| 45 | + } | |
| 46 | +} | |
| 47 | + | |
| 48 | +V8ScriptValue V8ScriptValueIterator::value() { | |
| 49 | + auto context = _context.Get(_engine->getIsolate()); | |
| 50 | + v8::Local<v8::Value> v8Value; | |
| 51 | + v8::Local<v8::Value> propertyName; | |
| 52 | + if (!_propertyNames.Get(_engine->getIsolate())->Get(context, _length).ToLocal(&propertyName)) { | |
| 53 | + Q_ASSERT(false); | |
| 54 | + } | |
| 55 | + if (!_object.Get(_engine->getIsolate())->Get(context, propertyName->ToString(context).ToLocalChecked()).ToLocal(&v8Value)) { | |
| 56 | + Q_ASSERT(false); | |
| 57 | + } | |
| 58 | + return V8ScriptValue(_engine->getIsolate(), v8Value); | |
| 59 | +} | |
| 60 | + | |
| 61 | +ScriptValue::PropertyFlags ScriptValueIteratorV8Wrapper::flags() const { | |
| 62 | + //V8TODO | |
| 63 | + //return (ScriptValue::PropertyFlags)(int)_value.flags(); | |
| 64 | + return (ScriptValue::PropertyFlags)(0); | |
| 65 | +} | |
| 66 | + | |
| 67 | +bool ScriptValueIteratorV8Wrapper::hasNext() const { | |
| 68 | + return _value->hasNext(); | |
| 69 | +} | |
| 70 | + | |
| 71 | +QString ScriptValueIteratorV8Wrapper::name() const { | |
| 72 | + return _value->name(); | |
| 73 | +} | |
| 74 | + | |
| 75 | +void ScriptValueIteratorV8Wrapper::next() { | |
| 76 | + _value->next(); | |
| 77 | +} | |
| 78 | + | |
| 79 | +ScriptValue ScriptValueIteratorV8Wrapper::value() const { | |
| 80 | + V8ScriptValue result = _value->value(); | |
| 81 | + return ScriptValue(new ScriptValueV8Wrapper(_engine, std::move(result))); | |
| 82 | +} | |