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