Skip to content

Commit a66cc42

Browse files
authored
feat: use node logic for globals and modules (#215)
* feat: add node helpers for interacting with functions * feat: use node logic for globals and modules * feat: allow passing of custom data for function templates
1 parent 5a6c2ee commit a66cc42

File tree

9 files changed

+647
-1
lines changed

9 files changed

+647
-1
lines changed

NativeScript/runtime/Caches.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ class Caches {
4848

4949
Caches(v8::Isolate* isolate, const int& isolateId_ = -1);
5050
~Caches();
51+
52+
bool isWorker = false;
5153

5254
static std::shared_ptr<ConcurrentMap<std::string, const Meta*>> Metadata;
5355
static std::shared_ptr<ConcurrentMap<int, std::shared_ptr<Caches::WorkerState>>> Workers;

NativeScript/runtime/Helpers.h

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,148 @@ void Assert(bool condition, v8::Isolate* isolate = nullptr, std::string const &r
198198

199199
void StopExecutionAndLogStackTrace(v8::Isolate* isolate);
200200

201+
202+
203+
204+
// Helpers from Node
205+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
206+
const char* data,
207+
int length) {
208+
return v8::String::NewFromOneByte(isolate,
209+
reinterpret_cast<const uint8_t*>(data),
210+
v8::NewStringType::kNormal,
211+
length).ToLocalChecked();
212+
}
213+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
214+
const signed char* data,
215+
int length) {
216+
return v8::String::NewFromOneByte(isolate,
217+
reinterpret_cast<const uint8_t*>(data),
218+
v8::NewStringType::kNormal,
219+
length).ToLocalChecked();
220+
}
221+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
222+
const unsigned char* data,
223+
int length) {
224+
return v8::String::NewFromOneByte(
225+
isolate, data, v8::NewStringType::kNormal, length)
226+
.ToLocalChecked();
227+
}
228+
229+
// Convenience wrapper around v8::String::NewFromOneByte().
230+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
231+
const char* data,
232+
int length = -1);
233+
// For the people that compile with -funsigned-char.
234+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
235+
const signed char* data,
236+
int length = -1);
237+
inline v8::Local<v8::String> OneByteString(v8::Isolate* isolate,
238+
const unsigned char* data,
239+
int length = -1);
240+
241+
242+
243+
v8::Local<v8::FunctionTemplate> NewFunctionTemplate(
244+
v8::Isolate* isolate,
245+
v8::FunctionCallback callback,
246+
v8::Local<v8::Value> data = v8::Local<v8::Value>(),
247+
v8::Local<v8::Signature> signature = v8::Local<v8::Signature>(),
248+
v8::ConstructorBehavior behavior = v8::ConstructorBehavior::kAllow,
249+
v8::SideEffectType side_effect = v8::SideEffectType::kHasSideEffect,
250+
const v8::CFunction* c_function = nullptr);
251+
// Convenience methods for NewFunctionTemplate().
252+
void SetMethod(v8::Local<v8::Context> context,
253+
v8::Local<v8::Object> that,
254+
const char* name,
255+
v8::FunctionCallback callback,
256+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
257+
// Similar to SetProtoMethod but without receiver signature checks.
258+
void SetMethod(v8::Isolate* isolate,
259+
v8::Local<v8::Template> that,
260+
const char* name,
261+
v8::FunctionCallback callback,
262+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
263+
void SetFastMethod(v8::Isolate* isolate,
264+
v8::Local<v8::Template> that,
265+
const char* name,
266+
v8::FunctionCallback slow_callback,
267+
const v8::CFunction* c_function,
268+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
269+
void SetFastMethod(v8::Local<v8::Context> context,
270+
v8::Local<v8::Object> that,
271+
const char* name,
272+
v8::FunctionCallback slow_callback,
273+
const v8::CFunction* c_function,
274+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
275+
void SetFastMethodNoSideEffect(v8::Isolate* isolate,
276+
v8::Local<v8::Template> that,
277+
const char* name,
278+
v8::FunctionCallback slow_callback,
279+
const v8::CFunction* c_function,
280+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
281+
void SetFastMethodNoSideEffect(v8::Local<v8::Context> context,
282+
v8::Local<v8::Object> that,
283+
const char* name,
284+
v8::FunctionCallback slow_callback,
285+
const v8::CFunction* c_function,
286+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
287+
void SetProtoMethod(v8::Isolate* isolate,
288+
v8::Local<v8::FunctionTemplate> that,
289+
const char* name,
290+
v8::FunctionCallback callback,
291+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
292+
void SetInstanceMethod(v8::Isolate* isolate,
293+
v8::Local<v8::FunctionTemplate> that,
294+
const char* name,
295+
v8::FunctionCallback callback,
296+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
297+
// Safe variants denote the function has no side effects.
298+
void SetMethodNoSideEffect(v8::Local<v8::Context> context,
299+
v8::Local<v8::Object> that,
300+
const char* name,
301+
v8::FunctionCallback callback,
302+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
303+
void SetProtoMethodNoSideEffect(v8::Isolate* isolate,
304+
v8::Local<v8::FunctionTemplate> that,
305+
const char* name,
306+
v8::FunctionCallback callback,
307+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
308+
void SetMethodNoSideEffect(v8::Isolate* isolate,
309+
v8::Local<v8::Template> that,
310+
const char* name,
311+
v8::FunctionCallback callback,
312+
v8::Local<v8::Value> data = v8::Local<v8::Value>());
313+
enum class SetConstructorFunctionFlag {
314+
NONE,
315+
SET_CLASS_NAME,
316+
};
317+
void SetConstructorFunction(v8::Local<v8::Context> context,
318+
v8::Local<v8::Object> that,
319+
const char* name,
320+
v8::Local<v8::FunctionTemplate> tmpl,
321+
SetConstructorFunctionFlag flag =
322+
SetConstructorFunctionFlag::SET_CLASS_NAME);
323+
void SetConstructorFunction(v8::Local<v8::Context> context,
324+
v8::Local<v8::Object> that,
325+
v8::Local<v8::String> name,
326+
v8::Local<v8::FunctionTemplate> tmpl,
327+
SetConstructorFunctionFlag flag =
328+
SetConstructorFunctionFlag::SET_CLASS_NAME);
329+
void SetConstructorFunction(v8::Isolate* isolate,
330+
v8::Local<v8::Template> that,
331+
const char* name,
332+
v8::Local<v8::FunctionTemplate> tmpl,
333+
SetConstructorFunctionFlag flag =
334+
SetConstructorFunctionFlag::SET_CLASS_NAME);
335+
void SetConstructorFunction(v8::Isolate* isolate,
336+
v8::Local<v8::Template> that,
337+
v8::Local<v8::String> name,
338+
v8::Local<v8::FunctionTemplate> tmpl,
339+
SetConstructorFunctionFlag flag =
340+
SetConstructorFunctionFlag::SET_CLASS_NAME);
341+
342+
201343
}
202344

203345
#endif /* Helpers_h */

0 commit comments

Comments
 (0)