1 /*
2  * Copyright (c) 2021-2022 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 "frameworks/bridge/declarative_frontend/engine/jsi/jsi_types.h"
17 
18 #include "base/log/ace_performance_monitor.h"
19 #include "frameworks/bridge/declarative_frontend/engine/jsi/js_ui_index.h"
20 #include "frameworks/bridge/js_frontend/engine/jsi/ark_js_runtime.h"
21 #include "frameworks/bridge/declarative_frontend/engine/jsi/jsi_declarative_engine.h"
22 
23 namespace OHOS::Ace::Framework {
24 
25 // -----------------------
26 // Implementation of JsiValue
27 // -----------------------
JsiValue(const panda::CopyableGlobal<panda::JSValueRef> & val)28 JsiValue::JsiValue(const panda::CopyableGlobal<panda::JSValueRef>& val) : JsiType(val) {}
29 
JsiValue(panda::Local<panda::JSValueRef> val)30 JsiValue::JsiValue(panda::Local<panda::JSValueRef> val) : JsiType(val) {}
31 
JsiValue(const EcmaVM * vm,panda::Local<panda::JSValueRef> val)32 JsiValue::JsiValue(const EcmaVM *vm, panda::Local<panda::JSValueRef> val) : JsiType(vm, val) {}
33 
IsEmpty() const34 bool JsiValue::IsEmpty() const
35 {
36     if (GetHandle().IsEmpty()) {
37         return true;
38     }
39     return GetHandle()->IsUndefined() || GetHandle()->IsNull();
40 }
41 
IsFunction() const42 bool JsiValue::IsFunction() const
43 {
44     if (GetHandle().IsEmpty()) {
45         return false;
46     } else {
47         return GetHandle()->IsFunction(GetEcmaVM());
48     }
49 }
50 
IsNumber() const51 bool JsiValue::IsNumber() const
52 {
53     if (GetHandle().IsEmpty()) {
54         return false;
55     } else {
56         return GetHandle()->IsNumber();
57     }
58 }
59 
IsString() const60 bool JsiValue::IsString() const
61 {
62     if (GetHandle().IsEmpty()) {
63         return false;
64     } else {
65         return GetHandle()->IsString(GetEcmaVM());
66     }
67 }
68 
IsBoolean() const69 bool JsiValue::IsBoolean() const
70 {
71     if (GetHandle().IsEmpty()) {
72         return false;
73     } else {
74         return GetHandle()->IsBoolean();
75     }
76 }
77 
IsObject() const78 bool JsiValue::IsObject() const
79 {
80     if (GetHandle().IsEmpty()) {
81         return false;
82     } else {
83         return GetHandle()->IsObject(GetEcmaVM());
84     }
85 }
86 
IsArray() const87 bool JsiValue::IsArray() const
88 {
89     if (GetHandle().IsEmpty()) {
90         return false;
91     } else {
92         return GetHandle()->IsArray(GetEcmaVM());
93     }
94 }
95 
IsArrayBuffer() const96 bool JsiValue::IsArrayBuffer() const
97 {
98     if (GetHandle().IsEmpty()) {
99         return false;
100     } else {
101         return GetHandle()->IsArrayBuffer(GetEcmaVM());
102     }
103 }
104 
IsUint8ClampedArray() const105 bool JsiValue::IsUint8ClampedArray() const
106 {
107     return (!GetHandle().IsEmpty()) && (GetHandle()->IsUint8ClampedArray(GetEcmaVM()));
108 }
109 
IsUndefined() const110 bool JsiValue::IsUndefined() const
111 {
112     if (GetHandle().IsEmpty()) {
113         return true;
114     } else {
115         return GetHandle()->IsUndefined();
116     }
117 }
118 
IsNull() const119 bool JsiValue::IsNull() const
120 {
121     if (GetHandle().IsEmpty()) {
122         return true;
123     } else {
124         return GetHandle()->IsNull();
125     }
126 }
127 
ToString() const128 std::string JsiValue::ToString() const
129 {
130     auto vm = GetEcmaVM();
131     panda::LocalScope scope(vm);
132     if (IsObject()) {
133         return JSON::Stringify(vm, GetLocalHandle())->ToString(vm)->ToString(vm);
134     }
135     return GetHandle()->ToString(vm)->ToString(vm);
136 }
137 
ToBoolean() const138 bool JsiValue::ToBoolean() const
139 {
140     return GetHandle()->BooleaValue(GetEcmaVM());
141 }
142 
Undefined()143 JsiRef<JsiValue> JsiValue::Undefined()
144 {
145     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
146     return JsiRef<JsiValue>::Make(panda::JSValueRef::Undefined(runtime->GetEcmaVm()));
147 }
148 
Null()149 JsiRef<JsiValue> JsiValue::Null()
150 {
151     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
152     return JsiRef<JsiValue>::Make(panda::JSValueRef::Null(runtime->GetEcmaVm()));
153 }
154 
True()155 JsiRef<JsiValue> JsiValue::True()
156 {
157     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
158     return JsiRef<JsiValue>::Make(panda::JSValueRef::True(runtime->GetEcmaVm()));
159 }
160 
False()161 JsiRef<JsiValue> JsiValue::False()
162 {
163     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
164     return JsiRef<JsiValue>::Make(panda::JSValueRef::False(runtime->GetEcmaVm()));
165 }
166 
167 // -----------------------
168 // Implementation of JsiArray
169 // -----------------------
JsiArray()170 JsiArray::JsiArray() {}
JsiArray(const panda::CopyableGlobal<panda::ArrayRef> & val)171 JsiArray::JsiArray(const panda::CopyableGlobal<panda::ArrayRef>& val) : JsiType(val) {}
JsiArray(panda::Local<panda::ArrayRef> val)172 JsiArray::JsiArray(panda::Local<panda::ArrayRef> val) : JsiType(val) {}
JsiArray(const EcmaVM * vm,panda::Local<panda::ArrayRef> val)173 JsiArray::JsiArray(const EcmaVM *vm, panda::Local<panda::ArrayRef> val) : JsiType(vm, val) {}
174 
GetValueAt(size_t index) const175 JsiRef<JsiValue> JsiArray::GetValueAt(size_t index) const
176 {
177     return JsiRef<JsiValue>::FastMake(GetEcmaVM(), panda::ArrayRef::GetValueAt(GetEcmaVM(), GetLocalHandle(), index));
178 }
179 
SetValueAt(size_t index,JsiRef<JsiValue> value) const180 void JsiArray::SetValueAt(size_t index, JsiRef<JsiValue> value) const
181 {
182     panda::ArrayRef::SetValueAt(GetEcmaVM(), GetLocalHandle(), index, value.Get().GetLocalHandle());
183 }
184 
GetProperty(const char * prop) const185 JsiRef<JsiValue> JsiArray::GetProperty(const char* prop) const
186 {
187     auto vm = GetEcmaVM();
188     auto stringRef = panda::StringRef::NewFromUtf8(vm, prop);
189     auto value = GetHandle()->Get(vm, stringRef);
190     auto func = JsiValue(vm, value);
191     auto refValue =  JsiRef<JsiValue>(func);
192     return refValue;
193 }
194 
GetProperty(int32_t propertyIndex) const195 JsiRef<JsiValue> JsiArray::GetProperty(int32_t propertyIndex) const
196 {
197     auto vm = GetEcmaVM();
198     auto stringRef = panda::ExternalStringCache::GetCachedString(vm, propertyIndex);
199     auto value = GetHandle()->Get(vm, stringRef);
200     auto func = JsiValue(vm, value);
201     auto refValue = JsiRef<JsiValue>(func);
202     return refValue;
203 }
204 
Length() const205 size_t JsiArray::Length() const
206 {
207     size_t length = -1;
208     JsiRef<JsiValue> propLength = GetProperty(static_cast<int32_t>(ArkUIIndex::LENGTH));
209     if (propLength->IsNumber()) {
210         length = propLength->ToNumber<int32_t>();
211     }
212     return length;
213 }
214 
SetLength(size_t length) const215 void JsiArray::SetLength(size_t length) const
216 {
217     auto stringRef = panda::StringRef::NewFromUtf8(GetEcmaVM(), "length");
218     GetHandle()->Set(GetEcmaVM(), stringRef, JsiValueConvertor::toJsiValueWithVM<size_t>(GetEcmaVM(), length));
219 }
220 
IsArray() const221 bool JsiArray::IsArray() const
222 {
223     if (GetHandle().IsEmpty()) {
224         return false;
225     } else {
226         return GetHandle()->IsArray(GetEcmaVM());
227     }
228 }
229 
230 // -----------------------
231 // Implementation of JsiArrayBuffer
232 // -----------------------
JsiArrayBuffer(panda::Local<panda::ArrayBufferRef> val)233 JsiArrayBuffer::JsiArrayBuffer(panda::Local<panda::ArrayBufferRef> val) : JsiType(val) {}
JsiArrayBuffer(const panda::CopyableGlobal<panda::ArrayBufferRef> & val)234 JsiArrayBuffer::JsiArrayBuffer(const panda::CopyableGlobal<panda::ArrayBufferRef>& val) : JsiType(val) {}
235 
ByteLength() const236 int32_t JsiArrayBuffer::ByteLength() const
237 {
238     return GetHandle()->ByteLength(GetEcmaVM());
239 }
240 
GetBuffer() const241 void* JsiArrayBuffer::GetBuffer() const
242 {
243     return GetHandle()->GetBuffer(GetEcmaVM());
244 }
245 
Detach() const246 void JsiArrayBuffer::Detach() const
247 {
248     GetHandle()->Detach(GetEcmaVM());
249 }
250 
IsDetach() const251 bool JsiArrayBuffer::IsDetach() const
252 {
253     return GetHandle()->IsDetach(GetEcmaVM());
254 }
255 
256 // -----------------------
257 // Implementation of JsiArrayBufferRef
258 // -----------------------
JsiUint8ClampedArray(panda::Local<panda::Uint8ClampedArrayRef> val)259 JsiUint8ClampedArray::JsiUint8ClampedArray(panda::Local<panda::Uint8ClampedArrayRef> val) : JsiType(val) {}
JsiUint8ClampedArray(const panda::CopyableGlobal<panda::Uint8ClampedArrayRef> & val)260 JsiUint8ClampedArray::JsiUint8ClampedArray(const panda::CopyableGlobal<panda::Uint8ClampedArrayRef>& val) : JsiType(val)
261 {}
262 
GetArrayBuffer() const263 JsiRef<JsiArrayBuffer> JsiUint8ClampedArray::GetArrayBuffer() const
264 {
265     return JsiRef<JsiArrayBuffer>(JsiArrayBuffer(GetHandle()->GetArrayBuffer(GetEcmaVM())));
266 }
267 
268 // -----------------------
269 // Implementation of JsiObject
270 // -----------------------
JsiObject()271 JsiObject::JsiObject() : JsiType() {}
JsiObject(const panda::CopyableGlobal<panda::ObjectRef> & val)272 JsiObject::JsiObject(const panda::CopyableGlobal<panda::ObjectRef>& val) : JsiType(val) {}
JsiObject(panda::Local<panda::ObjectRef> val)273 JsiObject::JsiObject(panda::Local<panda::ObjectRef> val) : JsiType(val) {}
JsiObject(const EcmaVM * vm,panda::Local<panda::ObjectRef> val)274 JsiObject::JsiObject(const EcmaVM *vm, panda::Local<panda::ObjectRef> val) : JsiType(vm, val) {}
275 
IsUndefined() const276 bool JsiObject::IsUndefined() const
277 {
278     if (GetHandle().IsEmpty()) {
279         return true;
280     } else {
281         return GetHandle()->IsUndefined();
282     }
283 }
284 
GetPropertyNames() const285 JsiRef<JsiArray> JsiObject::GetPropertyNames() const
286 {
287     auto vm = GetEcmaVM();
288     return JsiRef<JsiArray>::Make(GetHandle()->GetOwnPropertyNames(vm));
289 }
290 
GetProperty(int32_t propertyIndex) const291 JsiRef<JsiValue> JsiObject::GetProperty(int32_t propertyIndex) const
292 {
293     auto vm = GetEcmaVM();
294     auto str = panda::ExternalStringCache::GetCachedString(vm, propertyIndex);
295     auto value = GetHandle()->Get(vm, str);
296     auto func = JsiValue(vm, value);
297     auto refValue = JsiRef<JsiValue>(func);
298     return refValue;
299 }
300 
HasProperty(int32_t propertyIndex) const301 bool JsiObject::HasProperty(int32_t propertyIndex) const
302 {
303     auto vm = GetEcmaVM();
304     auto stringRef = panda::ExternalStringCache::GetCachedString(vm, propertyIndex);
305     bool has = GetHandle()->Has(vm, stringRef);
306     return has;
307 }
308 
GetProperty(const char * prop) const309 JsiRef<JsiValue> JsiObject::GetProperty(const char* prop) const
310 {
311     auto vm = GetEcmaVM();
312     auto stringRef = panda::StringRef::NewFromUtf8(vm, prop);
313     auto value = GetHandle()->Get(vm, stringRef);
314     auto func = JsiValue(vm, value);
315     auto refValue =  JsiRef<JsiValue>(func);
316     return refValue;
317 }
318 
HasProperty(const char * prop) const319 bool JsiObject::HasProperty(const char* prop) const
320 {
321     auto vm = GetEcmaVM();
322     auto stringRef = panda::StringRef::NewFromUtf8(vm, prop);
323     bool has = GetHandle()->Has(vm, stringRef);
324     return has;
325 }
326 
ToJsonObject(const char * value) const327 JsiRef<JsiValue> JsiObject::ToJsonObject(const char* value) const
328 {
329     auto vm = GetEcmaVM();
330     panda::TryCatch trycatch(vm);
331     auto valueRef = JsiValueConvertor::toJsiValueWithVM<std::string>(vm, value);
332     panda::Local<JSValueRef> result = JSON::Parse(vm, valueRef);
333     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
334     if (result.IsEmpty() || trycatch.HasCaught()) {
335         runtime->HandleUncaughtException(trycatch);
336         return JsiRef<JsiValue>::Make(JSValueRef::Undefined(vm));
337     }
338 
339     return JsiRef<JsiValue>::Make(result);
340 }
341 
SetPropertyJsonObject(const char * prop,const char * value) const342 void JsiObject::SetPropertyJsonObject(const char* prop, const char* value) const
343 {
344     auto vm = GetEcmaVM();
345     auto stringRef = panda::StringRef::NewFromUtf8(vm, prop);
346     auto valueRef = JsiValueConvertor::toJsiValueWithVM<std::string>(GetEcmaVM(), value);
347     if (valueRef->IsString(vm)) {
348         GetHandle()->Set(vm, stringRef, JSON::Parse(vm, valueRef));
349     }
350 }
351 
SetPropertyObject(const char * prop,JsiRef<JsiValue> value) const352 void JsiObject::SetPropertyObject(const char* prop, JsiRef<JsiValue> value) const
353 {
354     auto vm = GetEcmaVM();
355     auto stringRef = panda::StringRef::NewFromUtf8(vm, prop);
356     GetHandle()->Set(vm, stringRef, value.Get().GetLocalHandle());
357 }
358 
359 // -----------------------
360 // Implementation of JsiFunction
361 // -----------------------
JsiFunction()362 JsiFunction::JsiFunction() {}
JsiFunction(const panda::CopyableGlobal<panda::FunctionRef> & val)363 JsiFunction::JsiFunction(const panda::CopyableGlobal<panda::FunctionRef>& val) : JsiType(val) {}
364 
JsiFunction(panda::Local<panda::FunctionRef> val)365 JsiFunction::JsiFunction(panda::Local<panda::FunctionRef> val) : JsiType(val) {}
366 
JsiFunction(const EcmaVM * vm,panda::Local<panda::FunctionRef> val)367 JsiFunction::JsiFunction(const EcmaVM *vm, panda::Local<panda::FunctionRef> val) : JsiType(vm, val) {}
368 
Call(JsiRef<JsiValue> thisVal,int argc,JsiRef<JsiValue> argv[]) const369 JsiRef<JsiValue> JsiFunction::Call(JsiRef<JsiValue> thisVal, int argc, JsiRef<JsiValue> argv[]) const
370 {
371     JS_CALLBACK_DURATION();
372     auto vm = GetEcmaVM();
373     panda::JsiFastNativeScope fastNativeScope(vm);
374     LocalScope scope(vm);
375     panda::TryCatch trycatch(vm);
376     bool traceEnabled = false;
377     if (SystemProperties::GetDebugEnabled()) {
378         traceEnabled = AceTraceBeginWithArgs("ExecuteJS[%s]", GetHandle()->GetName(vm)->ToString(vm).c_str());
379     }
380     std::vector<panda::Local<panda::JSValueRef>> arguments;
381     for (int i = 0; i < argc; ++i) {
382         arguments.emplace_back(argv[i].Get().GetLocalHandle());
383     }
384     auto thisObj = thisVal.Get().GetLocalHandle();
385     auto result = GetHandle()->Call(vm, thisObj, arguments.data(), argc);
386     JSNApi::ExecutePendingJob(vm);
387     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
388     if (result.IsEmpty() || trycatch.HasCaught()) {
389         runtime->HandleUncaughtException(trycatch);
390         result = JSValueRef::Undefined(vm);
391     }
392     if (traceEnabled) {
393         AceTraceEnd();
394     }
395     return JsiRef<JsiValue>::Make(result);
396 }
397 
New(JsiFunctionCallback func)398 panda::Local<panda::FunctionRef> JsiFunction::New(JsiFunctionCallback func)
399 {
400     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
401     return panda::FunctionRef::New(const_cast<EcmaVM*>(runtime->GetEcmaVm()), func);
402 }
403 
404 // -----------------------
405 // Implementation of JsiObjectTemplate
406 // -----------------------
JsiObjTemplate(const panda::CopyableGlobal<panda::ObjectRef> & val)407 JsiObjTemplate::JsiObjTemplate(const panda::CopyableGlobal<panda::ObjectRef>& val) : JsiObject(val) {}
JsiObjTemplate(panda::Local<panda::ObjectRef> val)408 JsiObjTemplate::JsiObjTemplate(panda::Local<panda::ObjectRef> val) : JsiObject(val) {}
409 
SetInternalFieldCount(int32_t count) const410 void JsiObjTemplate::SetInternalFieldCount(int32_t count) const
411 {
412     GetHandle()->SetNativePointerFieldCount(GetEcmaVM(), count);
413 }
414 
NewInstance() const415 JsiRef<JsiObject> JsiObjTemplate::NewInstance() const
416 {
417     auto instance = panda::ObjectRef::New(GetEcmaVM());
418     instance->SetNativePointerFieldCount(GetEcmaVM(), 1);
419     return JsiRef<JsiObject>::Make(instance);
420 }
421 
New()422 panda::Local<panda::JSValueRef> JsiObjTemplate::New()
423 {
424     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
425     return panda::ObjectRef::New(runtime->GetEcmaVm());
426 }
427 
428 // -----------------------
429 // Implementation of JsiCallBackInfo
430 // -----------------------
JsiCallbackInfo(panda::JsiRuntimeCallInfo * info)431 JsiCallbackInfo::JsiCallbackInfo(panda::JsiRuntimeCallInfo* info) : info_(info) {}
432 
operator [](size_t index) const433 JsiRef<JsiValue> JsiCallbackInfo::operator[](size_t index) const
434 {
435     if (static_cast<int32_t>(index) < Length()) {
436         return JsiRef<JsiValue>::FastMake(info_->GetVM(), info_->GetCallArgRef(index));
437     }
438     return JsiRef<JsiValue>::FastMake(info_->GetVM(), panda::JSValueRef::Undefined(info_->GetVM()));
439 }
440 
441 JsiRef<JsiObject> JsiCallbackInfo::This() const
__anon79a814a20202(size_t index) 442 {
443     auto obj = JsiObject { info_->GetVM(), info_->GetThisRef() };
444     auto ref = JsiRef<JsiObject>(obj);
445     return ref;
446 }
447 
448 int JsiCallbackInfo::Length() const
__anon79a814a20302(size_t index) 449 {
450     return info_->GetArgsNumber();
451 }
452 
453 void JsiCallbackInfo::ReturnSelf() const
__anon79a814a20402(size_t index) 454 {
455     panda::CopyableGlobal<panda::JSValueRef> thisObj(info_->GetVM(), info_->GetThisRef());
456     retVal_ = thisObj;
457 }
458 
459 bool JsiCallbackInfo::GetBooleanArg(size_t index, bool& value) const
__anon79a814a20502(size_t index) 460 {
461     auto arg = info_->GetCallArgRef(index);
462     if (arg.IsEmpty() || !arg->IsBoolean()) {
463         return false;
464     }
465     value = arg->ToBoolean(info_->GetVM())->Value();
466     return true;
467 }
468 
469 bool JsiCallbackInfo::GetInt32Arg(size_t index, int32_t& value) const
__anon79a814a20602(size_t index) 470 {
471     auto arg = info_->GetCallArgRef(index);
472     if (arg.IsEmpty() || !arg->IsNumber()) {
473         return false;
474     }
475     value = arg->Int32Value(info_->GetVM());
476     return true;
477 }
478 
479 bool JsiCallbackInfo::GetUint32Arg(size_t index, uint32_t& value) const
__anon79a814a20702(size_t index) 480 {
481     auto arg = info_->GetCallArgRef(index);
482     if (arg.IsEmpty() || !arg->IsNumber()) {
483         return false;
484     }
485     value = arg->Uint32Value(info_->GetVM());
486     return true;
487 }
488 
489 bool JsiCallbackInfo::GetDoubleArg(size_t index, double& value) const
__anon79a814a20802(size_t index) 490 {
491     auto arg = info_->GetCallArgRef(index);
492     if (arg.IsEmpty()) {
493         return false;
494     }
495     bool ret = false;
496     value = arg->GetValueDouble(ret);
497     return ret;
498 }
499 
500 bool JsiCallbackInfo::GetStringArg(size_t index, std::string& value) const
__anon79a814a20902(size_t index) 501 {
502     auto arg = info_->GetCallArgRef(index);
503     if (arg.IsEmpty() || !arg->IsString(info_->GetVM())) {
504         return false;
505     }
506     value = arg->ToString(info_->GetVM())->ToString(info_->GetVM());
507     return true;
508 }
509 
510 bool JsiCallbackInfo::GetDoubleArrayArg(size_t index, std::vector<double>& valueArr) const
__anon79a814a20a02(size_t index) 511 {
512     auto arg = info_->GetCallArgRef(index);
513     if (arg.IsEmpty() || !arg->IsArray(info_->GetVM())) {
514         return false;
515     }
516     auto arrayRef = Local<ArrayRef>(arg);
517     uint32_t length = arrayRef->Length(info_->GetVM());
518     valueArr.reserve(length);
519     for (uint32_t i = 0; i < length; ++i) {
520         auto jsDouble = panda::ArrayRef::GetValueAt(info_->GetVM(), arrayRef, i);
521         if (!jsDouble.IsEmpty() && jsDouble->IsNumber()) {
522             valueArr.emplace_back(jsDouble->ToNumber(info_->GetVM())->Value());
523         }
524     }
525     return true;
526 }
527 
528 // -----------------------
529 // Implementation of JsiString
530 // -----------------------
531 JsiString::JsiString(const panda::CopyableGlobal<panda::StringRef>& val) : JsiType(val) {}
JsiString(panda::Local<panda::StringRef> val)532 JsiString::JsiString(panda::Local<panda::StringRef> val) : JsiType(val) {}
533 
New(const char * str)534 panda::Local<panda::StringRef> JsiString::New(const char* str)
535 {
536     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
537     return panda::StringRef::NewFromUtf8(runtime->GetEcmaVm(), str);
538 }
539 
New(const std::string & str)540 panda::Local<panda::StringRef> JsiString::New(const std::string& str)
541 {
542     return JsiString::New(str.c_str());
543 }
544 
545 // -----------------------
546 // Implementation of JsiDate
547 // -----------------------
JsiDate(const panda::CopyableGlobal<panda::DateRef> & val)548 JsiDate::JsiDate(const panda::CopyableGlobal<panda::DateRef>& val) : JsiType(val) {}
JsiDate(panda::Local<panda::DateRef> val)549 JsiDate::JsiDate(panda::Local<panda::DateRef> val) : JsiType(val) {}
550 
New(double value)551 JsiRef<JsiValue> JsiDate::New(double value)
552 {
553     auto runtime = std::static_pointer_cast<ArkJSRuntime>(JsiDeclarativeEngineInstance::GetCurrentRuntime());
554     return JsiRef<JsiValue>::Make(panda::DateRef::New(runtime->GetEcmaVm(), value));
555 }
556 
557 } // namespace OHOS::Ace::Framework
558