• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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 #ifndef JSVM_UTILS_H
16 #define JSVM_UTILS_H
17 
18 #include "jsvm.h"
19 
20 #include <cstdint>
21 #include <cstdio>
22 #include <cstring>
23 #include <string>
24 
25 #include "gtest/gtest.h"
26 
27 extern JSVM_Env jsvm_env;
28 
29 #define BUF_SIZE 256
30 
31 void PrintException(JSVM_Env env, JSVM_Value exception, const char *call);
32 void CheckErrorAndException(JSVM_Env env, JSVM_Status returnStatus, const char *call);
33 
34 #define JSVMTEST_CALL(the_call)                              \
35     do {                                                     \
36         JSVM_Status status = (the_call);                     \
37         CheckErrorAndException(jsvm_env, status, #the_call); \
38     } while (0)
39 
40 // jsvm utils
41 namespace jsvm {
42 
43 class HandleScope {
44 public:
HandleScope(JSVM_Env jsvmEnv)45     explicit HandleScope(JSVM_Env jsvmEnv) : env(jsvmEnv)
46     {
47         OH_JSVM_OpenHandleScope(env, &scope);
48     }
~HandleScope()49     ~HandleScope()
50     {
51         OH_JSVM_CloseHandleScope(env, scope);
52     }
53 
54 private:
55     JSVM_Env env;
56     JSVM_HandleScope scope;
57 };
58 
59 class Value {
60 public:
Value(JSVM_Value value)61     explicit Value(JSVM_Value value) : value_(value) {}
62 
JSVM_Value()63     operator JSVM_Value() const
64     {
65         return value_;
66     }
67 
68 protected:
69     JSVM_Value value_;
70 };
71 
72 class Object : Value {
73 public:
Object(JSVM_Value jsvm_value)74     explicit Object(JSVM_Value jsvm_value) : Value(jsvm_value)
75     {
76         bool isObject = false;
77         OH_JSVM_IsObject(jsvm_env, this->value_, &isObject);
78         if (!isObject) {
79             GTEST_LOG_(ERROR) << "Reset SIGPIPE failed.";
80         }
81     }
82 
Get(const char * propName)83     JSVM_Value Get(const char *propName)
84     {
85         JSVM_Value result;
86         OH_JSVM_GetNamedProperty(jsvm_env, this->value_, propName, &result);
87         return result;
88     }
89 
Set(const char * propName,JSVM_Value propValue)90     void Set(const char *propName, JSVM_Value propValue)
91     {
92         OH_JSVM_SetNamedProperty(jsvm_env, this->value_, propName, propValue);
93     }
94 };
95 
96 // global functions
97 JSVM_Value Str(const char *s);
98 JSVM_Value Str(const std::string &stdString);
99 JSVM_Script Compile(JSVM_Value js_str, const uint8_t *cache = nullptr, size_t size = JSVM_AUTO_LENGTH);
100 JSVM_Script CompileWithName(JSVM_Value js_str, const char *name);
101 JSVM_Script Compile(const char *s, const uint8_t *cache = nullptr, size_t size = JSVM_AUTO_LENGTH);
102 JSVM_Script CompileWithName(const char *s, const char *name);
103 JSVM_Value Run(JSVM_Script script);
104 JSVM_Value Run(const char *s);
105 bool ToBoolean(JSVM_Value val);
106 double ToNumber(JSVM_Value val);
107 std::string ToString(JSVM_Value val);
108 JSVM_Value True();
109 JSVM_Value False();
110 JSVM_Value Undefined();
111 JSVM_Value Null();
112 JSVM_Value Int32(int32_t v);
113 JSVM_Value Int64(int64_t v);
114 JSVM_Value Uint32(uint32_t v);
115 JSVM_Value Double(double v);
116 JSVM_Value Object();
117 JSVM_Value Global();
118 // object property
119 JSVM_Value GetProperty(JSVM_Value object, JSVM_Value key);
120 JSVM_Value GetProperty(JSVM_Value object, const char *name);
121 void SetProperty(JSVM_Value object, JSVM_Value key, JSVM_Value value);
122 void SetProperty(JSVM_Value object, const char *name, JSVM_Value value);
123 // Get named property with name `name` from globalThis
124 JSVM_Value Global(const char *name);
125 // Get property with key `key` from globalThis
126 JSVM_Value Global(JSVM_Value key);
127 // Call function with normal function with empty argument list
128 JSVM_Value Call(JSVM_Value func);
129 // Call function with normal function with specified argument list
130 JSVM_Value Call(JSVM_Value func, JSVM_Value thisArg, std::initializer_list<JSVM_Value> args);
131 // Call function as constructor with empty argument list
132 JSVM_Value New(JSVM_Value constructor);
133 bool InstanceOf(JSVM_Value value, JSVM_Value constructor);
134 bool IsNull(JSVM_Value value);
135 bool IsUndefined(JSVM_Value value);
136 bool IsNullOrUndefined(JSVM_Value value);
137 bool IsBoolean(JSVM_Value value);
138 bool IsTrue(JSVM_Value value);
139 bool IsFalse(JSVM_Value value);
140 bool IsNumber(JSVM_Value value);
141 bool IsString(JSVM_Value value);
142 bool IsObject(JSVM_Value value);
143 bool IsBigInt(JSVM_Value value);
144 bool IsSymbol(JSVM_Value value);
145 bool IsFunction(JSVM_Value value);
146 bool IsArray(JSVM_Value value);
147 bool IsArraybuffer(JSVM_Value value);
148 bool IsPromise(JSVM_Value value);
149 bool IsWasmModuleObject(JSVM_Value value);
150 bool IsWebAssemblyInstance(JSVM_Value value);
151 bool IsWebAssemblyMemory(JSVM_Value value);
152 bool IsWebAssemblyTable(JSVM_Value value);
153 size_t StringLength(JSVM_Value str);
154 uint32_t ArrayLength(JSVM_Value array);
155 JSVM_Value ArrayAt(JSVM_Value array, uint32_t index);
156 JSVM_Value JsonParse(JSVM_Value str);
157 JSVM_Value JsonStringify(JSVM_Value obj);
158 bool Equals(JSVM_Value lhs, JSVM_Value rhs);
159 bool StrictEquals(JSVM_Value lhs, JSVM_Value rhs);
160 // This is a simple log function
161 JSVM_Value MyConsoleLog(JSVM_Env env, JSVM_CallbackInfo info);
162 void InstallMyConsoleLog(JSVM_Env env);
163 void TryTriggerOOM();
164 void TryTriggerFatalError(JSVM_VM vm);
165 void TryTriggerGC();
166 }  // namespace jsvm
167 
168 #endif  // JSVM_UTILS_H
169