1 /*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "v8_native_array.h"
17
V8NativeArray(V8NativeEngine * engine,v8::Local<v8::Value> value)18 V8NativeArray::V8NativeArray(V8NativeEngine* engine, v8::Local<v8::Value> value) : V8NativeObject(engine, value) {}
19
V8NativeArray(V8NativeEngine * engine,uint32_t length)20 V8NativeArray::V8NativeArray(V8NativeEngine* engine, uint32_t length)
21 : V8NativeArray(engine, v8::Array::New(engine->GetIsolate(), length))
22 {
23 }
24
~V8NativeArray()25 V8NativeArray::~V8NativeArray() {}
26
GetInterface(int interfaceId)27 void* V8NativeArray::GetInterface(int interfaceId)
28 {
29 return (NativeArray::INTERFACE_ID == interfaceId) ? (NativeArray*)this
30 : V8NativeObject::GetInterface(interfaceId);
31 }
32
SetElement(uint32_t index,NativeValue * value)33 bool V8NativeArray::SetElement(uint32_t index, NativeValue* value)
34 {
35 v8::Local<v8::Object> obj = value_;
36 v8::Local<v8::Value> val = *value;
37 auto setMaybe = obj->Set(engine_->GetContext(), index, val);
38 return setMaybe.FromMaybe(false);
39 }
40
GetElement(uint32_t index)41 NativeValue* V8NativeArray::GetElement(uint32_t index)
42 {
43 v8::Local<v8::Object> obj = value_;
44 auto getMaybe = obj->Get(engine_->GetContext(), index);
45 return V8NativeEngine::V8ValueToNativeValue(engine_, getMaybe.ToLocalChecked());
46 }
47
HasElement(uint32_t index)48 bool V8NativeArray::HasElement(uint32_t index)
49 {
50 v8::Local<v8::Object> obj = value_;
51 v8::Maybe<bool> hasMaybe = obj->Has(engine_->GetContext(), index);
52 return hasMaybe.FromMaybe(false);
53 }
54
DeleteElement(uint32_t index)55 bool V8NativeArray::DeleteElement(uint32_t index)
56 {
57 v8::Local<v8::Object> obj = value_;
58 v8::Maybe<bool> deleteMaybe = obj->Delete(engine_->GetContext(), index);
59 return deleteMaybe.FromMaybe(false);
60 }
61
GetLength()62 uint32_t V8NativeArray::GetLength()
63 {
64 v8::Local<v8::Array> obj = value_;
65 return obj->Length();
66 }
67