diff --git a/NativeScript/runtime/Caches.h b/NativeScript/runtime/Caches.h index 96958c53..364b6121 100644 --- a/NativeScript/runtime/Caches.h +++ b/NativeScript/runtime/Caches.h @@ -48,6 +48,8 @@ class Caches { Caches(v8::Isolate* isolate, const int& isolateId_ = -1); ~Caches(); + + bool isWorker = false; static std::shared_ptr> Metadata; static std::shared_ptr>> Workers; diff --git a/NativeScript/runtime/Helpers.h b/NativeScript/runtime/Helpers.h index 55464dd7..80d1e442 100644 --- a/NativeScript/runtime/Helpers.h +++ b/NativeScript/runtime/Helpers.h @@ -198,6 +198,148 @@ void Assert(bool condition, v8::Isolate* isolate = nullptr, std::string const &r void StopExecutionAndLogStackTrace(v8::Isolate* isolate); + + + +// Helpers from Node +inline v8::Local OneByteString(v8::Isolate* isolate, + const char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::NewStringType::kNormal, + length).ToLocalChecked(); +} +inline v8::Local OneByteString(v8::Isolate* isolate, + const signed char* data, + int length) { + return v8::String::NewFromOneByte(isolate, + reinterpret_cast(data), + v8::NewStringType::kNormal, + length).ToLocalChecked(); +} +inline v8::Local OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length) { + return v8::String::NewFromOneByte( + isolate, data, v8::NewStringType::kNormal, length) + .ToLocalChecked(); +} + +// Convenience wrapper around v8::String::NewFromOneByte(). +inline v8::Local OneByteString(v8::Isolate* isolate, + const char* data, + int length = -1); +// For the people that compile with -funsigned-char. +inline v8::Local OneByteString(v8::Isolate* isolate, + const signed char* data, + int length = -1); +inline v8::Local OneByteString(v8::Isolate* isolate, + const unsigned char* data, + int length = -1); + + + +v8::Local NewFunctionTemplate( + v8::Isolate* isolate, + v8::FunctionCallback callback, + v8::Local data = v8::Local(), + v8::Local signature = v8::Local(), + v8::ConstructorBehavior behavior = v8::ConstructorBehavior::kAllow, + v8::SideEffectType side_effect = v8::SideEffectType::kHasSideEffect, + const v8::CFunction* c_function = nullptr); +// Convenience methods for NewFunctionTemplate(). +void SetMethod(v8::Local context, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +// Similar to SetProtoMethod but without receiver signature checks. +void SetMethod(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +void SetFastMethod(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function, + v8::Local data = v8::Local()); +void SetFastMethod(v8::Local context, + v8::Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function, + v8::Local data = v8::Local()); +void SetFastMethodNoSideEffect(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function, + v8::Local data = v8::Local()); +void SetFastMethodNoSideEffect(v8::Local context, + v8::Local that, + const char* name, + v8::FunctionCallback slow_callback, + const v8::CFunction* c_function, + v8::Local data = v8::Local()); +void SetProtoMethod(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +void SetInstanceMethod(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +// Safe variants denote the function has no side effects. +void SetMethodNoSideEffect(v8::Local context, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +void SetProtoMethodNoSideEffect(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +void SetMethodNoSideEffect(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + v8::Local data = v8::Local()); +enum class SetConstructorFunctionFlag { + NONE, + SET_CLASS_NAME, +}; +void SetConstructorFunction(v8::Local context, + v8::Local that, + const char* name, + v8::Local tmpl, + SetConstructorFunctionFlag flag = + SetConstructorFunctionFlag::SET_CLASS_NAME); +void SetConstructorFunction(v8::Local context, + v8::Local that, + v8::Local name, + v8::Local tmpl, + SetConstructorFunctionFlag flag = + SetConstructorFunctionFlag::SET_CLASS_NAME); +void SetConstructorFunction(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::Local tmpl, + SetConstructorFunctionFlag flag = + SetConstructorFunctionFlag::SET_CLASS_NAME); +void SetConstructorFunction(v8::Isolate* isolate, + v8::Local that, + v8::Local name, + v8::Local tmpl, + SetConstructorFunctionFlag flag = + SetConstructorFunctionFlag::SET_CLASS_NAME); + + } #endif /* Helpers_h */ diff --git a/NativeScript/runtime/Helpers.mm b/NativeScript/runtime/Helpers.mm index f98b005d..db93fcb2 100644 --- a/NativeScript/runtime/Helpers.mm +++ b/NativeScript/runtime/Helpers.mm @@ -679,3 +679,283 @@ void tns::StopExecutionAndLogStackTrace(v8::Isolate* isolate) { Assert(false, isolate); } + + +namespace tns { +Local NewFunctionTemplate( + v8::Isolate* isolate, + v8::FunctionCallback callback, + Local data, + Local signature, + v8::ConstructorBehavior behavior, + v8::SideEffectType side_effect_type, + const v8::CFunction* c_function) { + return v8::FunctionTemplate::New(isolate, + callback, + data, + signature, + 0, + behavior, + side_effect_type, + c_function); +} +void SetMethod(Local context, + Local that, + const char* name, + v8::FunctionCallback callback, + Local data) { + Isolate* isolate = context->GetIsolate(); + Local function = + NewFunctionTemplate(isolate, + callback, + data, + Local(), + v8::ConstructorBehavior::kThrow, + v8::SideEffectType::kHasSideEffect) + ->GetFunction(context) + .ToLocalChecked(); + // kInternalized strings are created in the old space. + const v8::NewStringType type = v8::NewStringType::kInternalized; + Local name_string = + v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked(); + that->Set(context, name_string, function).Check(); + function->SetName(name_string); // NODE_SET_METHOD() compatibility. +} +void SetMethod(v8::Isolate* isolate, + v8::Local that, + const char* name, + v8::FunctionCallback callback, + Local data) { + Local t = + NewFunctionTemplate(isolate, + callback, + data, + Local(), + v8::ConstructorBehavior::kThrow, + v8::SideEffectType::kHasSideEffect); + // kInternalized strings are created in the old space. + const v8::NewStringType type = v8::NewStringType::kInternalized; + Local name_string = + v8::String::NewFromUtf8(isolate, name, type).ToLocalChecked(); + that->Set(name_string, t); +} +void SetFastMethod(Isolate* isolate, + Local