8 #ifndef META_OCEAN_INTERACTION_JS_BASE_H
9 #define META_OCEAN_INTERACTION_JS_BASE_H
33 class OCEAN_INTERACTION_JS_EXPORT
JSBase
42 static std::string
toAString(
const v8::Local<v8::String>& value);
49 static std::string
toAString(v8::MaybeLocal<v8::String> value);
57 static inline v8::Local<v8::String> newString(
const std::string& value, v8::Isolate* isolate);
65 static inline v8::Local<v8::String> newString(
const char* value, v8::Isolate* isolate);
76 template <
typename TNative>
77 static inline v8::Local<v8::Value> createValues(
const std::vector<TNative>& values,
const v8::Local<v8::Context>& context);
87 template <
typename TNative>
88 static v8::Local<v8::Value> createValues(
const TNative* values,
const size_t size,
const v8::Local<v8::Context>& context);
98 static v8::Local<v8::Value> createObject(
typename T::NativeType&& value,
const v8::Local<v8::Context>& context);
107 template <
typename T>
108 static v8::Local<v8::Value> createObject(
const typename T::NativeType& value,
const v8::Local<v8::Context>& context);
117 template <
typename T>
118 static v8::Local<v8::Array> createObjects(
const std::vector<typename T::NativeType>& values,
const v8::Local<v8::Context>& context);
128 template <
typename TNative>
129 static bool hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, TNative& value);
139 template <
typename TNative>
140 static bool hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<TNative>& value);
151 template <
typename TNative>
152 static unsigned int hasValues(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int startIndex,
const unsigned int endIndex, TNative* values);
161 template <
typename TNative>
162 static bool isValue(v8::Local<v8::Value> localValue, TNative& value);
169 template <
typename TNative>
170 static void constructor(
const v8::FunctionCallbackInfo<v8::Value>& info);
178 template <
typename TNative>
179 static void constructor(TNative& thisValue,
const v8::FunctionCallbackInfo<v8::Value>& info);
188 template <
typename T,
unsigned int tGetterId>
189 static inline void propertyGetter(v8::Local<v8::String> property,
const v8::PropertyCallbackInfo<v8::Value>& info);
200 template <
typename TNative,
unsigned int tGetterId>
201 static void propertyGetter(TNative& thisValue, v8::Local<v8::String>& property,
const v8::PropertyCallbackInfo<v8::Value>& info);
211 template <
typename T,
unsigned int tSetterId>
212 static inline void propertySetter(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info);
224 template <
typename TNative,
unsigned int tSetterId>
225 static void propertySetter(TNative& thisValue, v8::Local<v8::String>& property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info);
233 template <
typename T,
unsigned int tFunctionId>
234 static inline void function(
const v8::FunctionCallbackInfo<v8::Value>& info);
244 template <
typename T,
unsigned int tFunctionId>
245 static void function(T& thisValue,
const v8::FunctionCallbackInfo<v8::Value>& info);
249 inline v8::Local<v8::Value>
JSBase::createValues(
const std::vector<bool>& values,
const v8::Local<v8::Context>& context)
251 v8::Isolate* isolate = v8::Isolate::GetCurrent();
253 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(values.size()));
255 for (
size_t n = 0; n < values.size(); ++n)
257 if (!result->Set(context, v8::Int32::New(isolate,
int(n)), v8::Boolean::New(isolate, values[n])).IsJust())
259 ocean_assert(
false &&
"Invalid value!");
266 template <
typename TNative>
267 v8::Local<v8::Value>
JSBase::createValues(
const std::vector<TNative>& values,
const v8::Local<v8::Context>& context)
269 return createValues<TNative>(values.data(), values.size(), context);
273 inline v8::Local<v8::Value>
JSBase::createValues(
const int* values,
const size_t size,
const v8::Local<v8::Context>& context)
275 v8::Isolate* isolate = v8::Isolate::GetCurrent();
277 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(size));
279 for (
size_t n = 0; n < size; ++n)
281 if (!result->Set(context, v8::Int32::New(isolate,
int(n)), v8::Int32::New(isolate, values[n])).IsJust())
283 ocean_assert(
false &&
"Invalid value!");
291 inline v8::Local<v8::Value>
JSBase::createValues(
const float* values,
const size_t size,
const v8::Local<v8::Context>& context)
293 v8::Isolate* isolate = v8::Isolate::GetCurrent();
295 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(size));
297 for (
size_t n = 0; n < size; ++n)
299 if (!result->Set(context, v8::Int32::New(isolate,
int(n)), v8::Number::New(isolate,
double(values[n]))).IsJust())
301 ocean_assert(
false &&
"Invalid value!");
309 inline v8::Local<v8::Value>
JSBase::createValues(
const double* values,
const size_t size,
const v8::Local<v8::Context>& context)
311 v8::Isolate* isolate = v8::Isolate::GetCurrent();
313 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(size));
315 for (
size_t n = 0; n < size; ++n)
317 if (!result->Set(context, v8::Int32::New(isolate,
int(n)), v8::Number::New(isolate, values[n])).IsJust())
319 ocean_assert(
false &&
"Invalid value!");
327 inline v8::Local<v8::Value>
JSBase::createValues(
const std::string* values,
const size_t size,
const v8::Local<v8::Context>& context)
329 v8::Isolate* isolate = v8::Isolate::GetCurrent();
331 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(size));
333 for (
size_t n = 0; n < size; ++n)
335 if (!result->Set(context, v8::Int32::New(isolate,
int(n)),
newString(values[n], isolate)).IsJust())
337 ocean_assert(
false &&
"Invalid value!");
344 template <
typename TNative>
345 inline v8::Local<v8::Value>
JSBase::createValues(
const TNative* values,
const size_t size,
const v8::Local<v8::Context>& context)
347 static_assert(oceanFalse<TNative>(),
"This function is not defined for the data type");
350 template <
typename T>
351 v8::Local<v8::Value>
JSBase::createObject(
typename T::NativeType&& value,
const v8::Local<v8::Context>& context)
353 v8::MaybeLocal<v8::Object> maybeResult(T::objectTemplate()->NewInstance(context));
355 if (maybeResult.IsEmpty())
357 ocean_assert(
false &&
"Invalid object!");
358 return v8::Local<v8::Value>();
361 v8::Isolate* isolate = v8::Isolate::GetCurrent();
363 v8::Local<v8::Object> result(maybeResult.ToLocalChecked());
366 result->SetInternalField(0, v8::External::New(isolate, newExternal));
371 template <
typename T>
372 v8::Local<v8::Value>
JSBase::createObject(
const typename T::NativeType& value,
const v8::Local<v8::Context>& context)
374 v8::MaybeLocal<v8::Object> maybeResult(T::objectTemplate()->NewInstance(context));
376 if (maybeResult.IsEmpty())
378 ocean_assert(
false &&
"Invalid object!");
379 return v8::Local<v8::Value>();
382 v8::Isolate* isolate = v8::Isolate::GetCurrent();
384 v8::Local<v8::Object> result(maybeResult.ToLocalChecked());
387 result->SetInternalField(0, v8::External::New(isolate, newExternal));
392 template <
typename T>
393 v8::Local<v8::Array>
JSBase::createObjects(
const std::vector<typename T::NativeType>& values,
const v8::Local<v8::Context>& context)
395 v8::Isolate* isolate = v8::Isolate::GetCurrent();
397 v8::Local<v8::Array> result = v8::Array::New(isolate,
int(values.size()));
399 for (
size_t n = 0; n < values.size(); ++n)
401 if (!result->Set(context, v8::Int32::New(isolate,
int(n)), JSBase::createObject<T>(values[n], context)).IsJust())
403 ocean_assert(
false &&
"Invalid value!");
412 ocean_assert(isolate !=
nullptr);
416 return v8::Local<v8::String>();
419 v8::MaybeLocal<v8::String> maybeString = v8::String::NewFromUtf8(isolate, value.c_str());
420 ocean_assert(!maybeString.IsEmpty());
422 return maybeString.ToLocalChecked();
427 ocean_assert(isolate !=
nullptr);
429 if (value ==
nullptr)
431 return v8::Local<v8::String>();
434 v8::MaybeLocal<v8::String> maybeString = v8::String::NewFromUtf8(isolate, value);
435 ocean_assert(!maybeString.IsEmpty());
437 return maybeString.ToLocalChecked();
440 template <
typename TNative>
443 ocean_assert(info.This()->InternalFieldCount() == 1);
445 TNative* newValue =
new TNative();
447 constructor<TNative>(*newValue, info);
449 v8::Isolate* isolate = v8::Isolate::GetCurrent();
451 v8::Local<v8::Object> ob = info.This();
454 info.This()->SetInternalField(0, v8::External::New(isolate, newExternal));
455 info.GetReturnValue().Set(info.This());
458 template <
typename T,
unsigned int tFunctionId>
461 ocean_assert(info.Holder()->InternalFieldCount() == 1);
462 const v8::Local<v8::External> thisWrapper(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0)));
465 ocean_assert(thisExternal->
type() == JSExternal::type<T>());
467 propertyGetter<T, tFunctionId>(thisExternal->
value<T>(), property, info);
470 template <
typename T,
unsigned int tFunctionId>
471 inline void JSBase::propertySetter(v8::Local<v8::String> property, v8::Local<v8::Value> value,
const v8::PropertyCallbackInfo<void>& info)
473 ocean_assert(value.IsEmpty() ==
false);
474 ocean_assert(info.Holder()->InternalFieldCount() == 1);
476 const v8::Local<v8::External> thisWrapper(v8::Local<v8::External>::Cast(info.Holder()->GetInternalField(0)));
479 ocean_assert(thisExternal->
type() == JSExternal::type<T>());
481 propertySetter<T, tFunctionId>(thisExternal->
value<T>(), property, value, info);
484 template <
typename T,
unsigned int tFunctionId>
487 ocean_assert(info.This()->InternalFieldCount() == 1);
488 v8::Local<v8::External> thisWrapper(v8::Local<v8::External>::Cast(info.This()->GetInternalField(0)));
491 ocean_assert(thisExternal->
type() == JSExternal::type<T>());
493 function<T, tFunctionId>(thisExternal->
value<T>(), info);
497 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index,
bool& value)
499 if (info.Length() >=
int(index + 1u))
501 if (info[index]->IsBoolean())
503 #if defined(OCEAN_V8_VERSION) && OCEAN_V8_VERSION > 70000
504 value = info[index]->BooleanValue(v8::Isolate::GetCurrent());
517 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index,
int& value)
519 if (info.Length() >=
int(index + 1u))
521 if (info[index]->IsInt32())
533 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index,
double& value)
535 if (info.Length() >=
int(index + 1u))
537 if (info[index]->IsNumber())
549 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index,
float& value)
551 if (info.Length() >=
int(index + 1u))
553 if (info[index]->IsNumber())
565 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::string& value)
567 if (info.Length() >=
int(index + 1u))
569 if (info[index]->IsString())
580 template <
typename TNative>
581 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, TNative& value)
583 if (info.Length() >=
int(index + 1u))
587 if (externalParameter !=
nullptr && externalParameter->
type() == JSExternal::type<TNative>())
589 value = externalParameter->
value<TNative>();
599 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<bool>& value)
601 if (info.Length() >=
int(index + 1u))
603 if (info[index]->IsArray())
606 #if defined(OCEAN_V8_VERSION) && OCEAN_V8_VERSION > 70000
607 v8::Isolate* isolate = v8::Isolate::GetCurrent();
610 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
613 value.reserve(arrayValue->Length());
615 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
617 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
619 if (!element.IsEmpty() || !element.ToLocalChecked()->IsBoolean())
624 #if defined(OCEAN_V8_VERSION) && OCEAN_V8_VERSION > 70000
625 value.emplace_back(element.ToLocalChecked()->BooleanValue(isolate));
627 value.emplace_back(element.ToLocalChecked()->BooleanValue(currentContext).FromJust());
639 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<int>& value)
641 if (info.Length() >=
int(index + 1u))
643 if (info[index]->IsArray())
647 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
650 value.reserve(arrayValue->Length());
652 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
654 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
656 if (!element.IsEmpty() || !element.ToLocalChecked()->IsInt32())
661 value.emplace_back(element.ToLocalChecked()->Int32Value(currentContext).FromJust());
672 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<float>& value)
674 if (info.Length() >=
int(index + 1u))
676 if (info[index]->IsArray())
680 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
683 value.reserve(arrayValue->Length());
685 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
687 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
689 if (!element.IsEmpty() || !element.ToLocalChecked()->IsNumber())
694 value.emplace_back(
float(element.ToLocalChecked()->NumberValue(currentContext).FromJust()));
705 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<double>& value)
707 if (info.Length() >=
int(index + 1u))
709 if (info[index]->IsArray())
713 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
716 value.reserve(arrayValue->Length());
718 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
720 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
722 if (!element.IsEmpty() || !element.ToLocalChecked()->IsNumber())
727 value.emplace_back(element.ToLocalChecked()->NumberValue(currentContext).FromJust());
738 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<Timestamp>& value)
740 if (info.Length() >=
int(index + 1u))
742 if (info[index]->IsArray())
746 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
749 value.reserve(arrayValue->Length());
751 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
753 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
755 if (!element.IsEmpty() || !element.ToLocalChecked()->IsNumber())
760 value.emplace_back(element.ToLocalChecked()->NumberValue(currentContext).FromJust());
771 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<std::string>& value)
773 if (info.Length() >=
int(index + 1u))
775 if (info[index]->IsArray())
779 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
782 value.reserve(arrayValue->Length());
784 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
786 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
788 if (!element.IsEmpty() || !element.ToLocalChecked()->IsString())
793 value.emplace_back(
toAString(element.ToLocalChecked()->ToString(currentContext)));
803 template <
typename TNative>
804 inline bool JSBase::hasValue(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int index, std::vector<TNative>& value)
806 if (info.Length() >=
int(index + 1u))
808 if (info[index]->IsArray())
812 v8::Local<v8::Array> arrayValue = v8::Local<v8::Array>::Cast(info[index]);
815 value.reserve(arrayValue->Length());
817 for (
unsigned int n = 0; n < arrayValue->Length(); ++n)
819 v8::MaybeLocal<v8::Value> element = arrayValue->Get(currentContext, n);
821 if (!element.IsEmpty() || !element.ToLocalChecked()->IsObject())
826 value.emplace_back();
827 if (!JSBase::isValue<TNative>(element.ToLocalChecked(), value.back()))
841 inline unsigned int JSBase::hasValues(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int startIndex,
const unsigned int endIndex,
double* values)
843 ocean_assert(startIndex < endIndex);
844 ocean_assert(values !=
nullptr);
846 for (
unsigned int index = startIndex; index < endIndex; ++index)
848 if (info.Length() >=
int(index + 1u))
850 if (info[index]->IsNumber())
857 return index - startIndex;
860 return endIndex - startIndex;
864 inline unsigned int JSBase::hasValues(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int startIndex,
const unsigned int endIndex,
float* values)
866 ocean_assert(startIndex < endIndex);
867 ocean_assert(values !=
nullptr);
869 for (
unsigned int index = startIndex; index < endIndex; ++index)
871 if (info.Length() >=
int(index + 1u))
873 if (info[index]->IsNumber())
880 return index - startIndex;
883 return endIndex - startIndex;
886 template <
typename TNative>
887 inline unsigned int JSBase::hasValues(
const v8::FunctionCallbackInfo<v8::Value>& info,
const unsigned int startIndex,
const unsigned int endIndex, TNative* values)
889 ocean_assert(startIndex < endIndex);
890 ocean_assert(values !=
nullptr);
892 for (
unsigned int index = startIndex; index < endIndex; ++index)
894 if (info.Length() >=
int(index + 1u))
896 if (JSBase::isValue<TNative>(info[index], values[index - startIndex]))
902 return index - startIndex;
905 return endIndex - startIndex;
911 if (localValue->IsBoolean())
914 #if defined(OCEAN_V8_VERSION) && OCEAN_V8_VERSION > 70000
915 value = localValue->BooleanValue(v8::Isolate::GetCurrent());
931 if (localValue->IsNumber())
946 if (localValue->IsNumber())
961 if (localValue->IsString())
973 template <
typename TNative>
978 if (externalParameter !=
nullptr && externalParameter->
type() == JSExternal::type<TNative>())
980 value = externalParameter->
value<TNative>();
This class implements the base class for all JavaScript wrappers.
Definition: JSBase.h:34
static void propertyGetter(TNative &thisValue, v8::Local< v8::String > &property, const v8::PropertyCallbackInfo< v8::Value > &info)
The specialized callback function for all JavaScript getters of wrapped C++ objects.
static v8::Local< v8::Value > createValues(const std::vector< TNative > &values, const v8::Local< v8::Context > &context)
Creates an JavaScript array value for a given C++ array.
Definition: JSBase.h:267
static bool isValue(v8::Local< v8::Value > localValue, TNative &value)
Returns whether a JavaScript value wraps a specified native type.
Definition: JSBase.h:974
static void propertySetter(v8::Local< v8::String > property, v8::Local< v8::Value > value, const v8::PropertyCallbackInfo< void > &info)
The callback function for all JavaScript setters of wrapped C++ objects.
Definition: JSBase.h:471
static v8::Local< v8::Value > createObject(typename T::NativeType &&value, const v8::Local< v8::Context > &context)
Creates an JavaScript value for a given C++ wrapper object.
Definition: JSBase.h:351
static std::string toAString(v8::MaybeLocal< v8::String > value)
Returns the std string of a given JavaScript string object.
static v8::Local< v8::String > newString(const std::string &value, v8::Isolate *isolate)
Creates a JavaScript string object from a given std string.
Definition: JSBase.h:410
static v8::Local< v8::Array > createObjects(const std::vector< typename T::NativeType > &values, const v8::Local< v8::Context > &context)
Creates an JavaScript array value for given C++ wrapper objects.
Definition: JSBase.h:393
static void propertySetter(TNative &thisValue, v8::Local< v8::String > &property, v8::Local< v8::Value > value, const v8::PropertyCallbackInfo< void > &info)
The specialized callback function for all JavaScript setters of wrapped C++ objects.
static void propertyGetter(v8::Local< v8::String > property, const v8::PropertyCallbackInfo< v8::Value > &info)
The callback function for all JavaScript getters of wrapped C++ objects.
Definition: JSBase.h:459
static void constructor(TNative &thisValue, const v8::FunctionCallbackInfo< v8::Value > &info)
The specialized callback function for all JavaScript constructors of wrapped C++ objects.
static unsigned int hasValues(const v8::FunctionCallbackInfo< v8::Value > &info, const unsigned int startIndex, const unsigned int endIndex, TNative *values)
Returns whether a JavaScript function call holds a sequence of specific native type as parameters.
Definition: JSBase.h:887
static void constructor(const v8::FunctionCallbackInfo< v8::Value > &info)
The callback function for all JavaScript constructors of wrapped C++ objects.
Definition: JSBase.h:441
static std::string toAString(const v8::Local< v8::String > &value)
Returns the std string of a given JavaScript string object.
static bool hasValue(const v8::FunctionCallbackInfo< v8::Value > &info, const unsigned int index, TNative &value)
Returns whether a JavaScript function call holds a specific native type as parameter.
Definition: JSBase.h:581
static void function(const v8::FunctionCallbackInfo< v8::Value > &info)
The callback function for all JavaScript functions of wrapped C++ objects.
Definition: JSBase.h:485
static const v8::Local< v8::Context > & currentContext()
Returns the current JavaScript context object.
Definition: JSContext.h:337
This class implements a type-safe object wrapper for non-JavaScript objects.
Definition: JSExternal.h:50
Type type() const
Returns the type of the wrapped C++ object.
Definition: JSExternal.h:215
static JSExternal * create(TNative *data, v8::Local< v8::Object > &owner, v8::Isolate *isolate)
Creates a new type-safe wrapper object for a specific native C++ object.
Definition: JSExternal.h:210
TNative & value()
Returns the wrapped C++ object.
Definition: JSExternal.h:383
static JSExternal * external(const v8::Local< v8::Value > &value)
Returns the external object from a given JavaScript object.
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15