diff --git a/src/v8/isolate_scope.rs b/src/v8/isolate_scope.rs index b8fd66a..d317b1e 100644 --- a/src/v8/isolate_scope.rs +++ b/src/v8/isolate_scope.rs @@ -38,7 +38,7 @@ pub struct V8IsolateScope<'isolate> { } extern "C" fn free_external_data(arg1: *mut ::std::os::raw::c_void) { - unsafe { Box::from_raw(arg1 as *mut T) }; + let _ = unsafe { Box::from_raw(arg1 as *mut T) }; } #[derive(Debug, Clone, Copy)] diff --git a/v8_c_api/src/v8_c_api.cpp b/v8_c_api/src/v8_c_api.cpp index a49fc7d..b5fbda2 100644 --- a/v8_c_api/src/v8_c_api.cpp +++ b/v8_c_api/src/v8_c_api.cpp @@ -9,16 +9,24 @@ #include "libplatform/libplatform.h" #include +#include -static v8::Platform* platform = NULL; +namespace { +v8::Platform* GLOBAL_PLATFORM = NULL; +} // anonymous namespace +/// Our slot is a slot where we store our own data. The 0th index of +/// V8 is forbidden from being used, so we store our data at this index +/// instead. +#define OUR_SLOT 1 +#define DEBUGGER_INDEX 1 /// Returns the corrected index. The index passed is expected to be an /// index relative to the user data. However, the first elements we store /// aren't actually the user data, but our internal data. So the user /// shouldn't be allowed to set or get the internal data, and for that /// purpose we should always correct the index which should point to /// real data location. -#define INTERNAL_OFFSET 2 +#define INTERNAL_OFFSET 2 + OUR_SLOT #define DATA_INDEX(user_index) (user_index + INTERNAL_OFFSET) extern "C" { @@ -27,161 +35,170 @@ extern "C" { #include #include -static v8_alloctor DefaultAllocator = { - .v8_Alloc = malloc, - .v8_Realloc = realloc, - .v8_Free = free, - .v8_Calloc = calloc, - .v8_Strdup = strdup, +static v8_allocator DefaultAllocator = { + .v8_Alloc = malloc, + .v8_Realloc = realloc, + .v8_Free = free, + .v8_Calloc = calloc, + .v8_Strdup = strdup, }; -static v8_alloctor *allocator; +static v8_allocator *allocator; #define V8_ALLOC allocator->v8_Alloc #define V8_REALLOC allocator->v8_Realloc #define V8_FREE allocator->v8_Free #define V8_CALLOC allocator->v8_Calloc #define V8_STRDUP allocator->v8_Strdup + struct v8_isolate_scope { - v8::Isolate *isolate; - v8::Locker locker; - v8_isolate_scope(v8::Isolate *v8_isolate): locker(v8_isolate), isolate(v8_isolate) {} - ~v8_isolate_scope() {} + v8::Isolate *isolate; + v8::Locker locker; + v8_isolate_scope(v8::Isolate *v8_isolate): locker(v8_isolate), isolate(v8_isolate) {} + ~v8_isolate_scope() {} }; struct v8_context { - v8::Isolate *isolate; - v8::Persistent *persistent_ctx; + v8::Isolate *isolate; + v8::Persistent *persistent_ctx; }; struct v8_handlers_scope { - v8::HandleScope handle_scope; - v8_handlers_scope(v8::Isolate *v8_isolate): handle_scope(v8_isolate){} + v8::HandleScope handle_scope; + v8_handlers_scope(v8::Isolate *v8_isolate): handle_scope(v8_isolate){} }; struct v8_local_string { - v8::Local str; - v8_local_string(v8::Isolate *isolate, const char *buff, size_t len) { - str = v8::String::NewFromUtf8(isolate, buff, v8::NewStringType::kNormal, len).ToLocalChecked(); - } - v8_local_string(v8::Local val): str(val) {} - ~v8_local_string() {} + v8::Local str; + v8_local_string(v8::Isolate *isolate, const char *buff, size_t len) { + str = v8::String::NewFromUtf8(isolate, buff, v8::NewStringType::kNormal, len).ToLocalChecked(); + } + v8_local_string(v8::Local val): str(val) {} + ~v8_local_string() {} + + inline std::string toString(v8::Isolate *isolate) const { + if (!isolate) { + isolate = v8::Isolate::GetCurrent(); + } + v8::String::Utf8Value s(isolate, str); + return std::string(*s); + } }; struct v8_local_script { - v8::Local script; - v8_local_script(v8::Local v8_local_ctx, v8_local_string *code) { - v8::MaybeLocal compilation_res = v8::Script::Compile(v8_local_ctx, code->str); - if (!compilation_res.IsEmpty()) { - script = compilation_res.ToLocalChecked(); - } - } - v8_local_script(v8::Local s): script(s) {} + v8::Local script; + v8_local_script(v8::Local v8_local_ctx, v8_local_string *code) { + v8::MaybeLocal compilation_res = v8::Script::Compile(v8_local_ctx, code->str); + if (!compilation_res.IsEmpty()) { + script = compilation_res.ToLocalChecked(); + } + } + v8_local_script(v8::Local s): script(s) {} }; struct v8_local_module { - v8::Local mod; - v8_local_module(v8::Local m): mod(m) {} - v8_local_module(v8::Isolate *isolate, v8::Persistent *m) { - mod = v8::Local::New(isolate, *m); - } + v8::Local mod; + v8_local_module(v8::Local m): mod(m) {} + v8_local_module(v8::Isolate *isolate, v8::Persistent *m) { + mod = v8::Local::New(isolate, *m); + } }; struct v8_local_value { - v8::Local val; - v8_local_value(v8::Local value): val(value) {} - v8_local_value(v8::Isolate *isolate, v8::Persistent *value) { - val = v8::Local::New(isolate, *value); - } + v8::Local val; + v8_local_value(v8::Local value): val(value) {} + v8_local_value(v8::Isolate *isolate, v8::Persistent *value) { + val = v8::Local::New(isolate, *value); + } }; struct v8_utf8_value { - v8::String::Utf8Value utf8_val; - v8_utf8_value(v8::Isolate *isolate, v8::Local val): utf8_val(isolate, val) {} + v8::String::Utf8Value utf8_val; + v8_utf8_value(v8::Isolate *isolate, v8::Local val): utf8_val(isolate, val) {} }; struct v8_local_native_function_template { - v8::Local func; - v8_local_native_function_template(v8::Local f): func(f) {} + v8::Local func; + v8_local_native_function_template(v8::Local f): func(f) {} }; struct v8_local_native_function { - v8::Local func; - v8_local_native_function(v8::Local f): func(f) {} + v8::Local func; + v8_local_native_function(v8::Local f): func(f) {} }; struct v8_local_object_template { - v8::Local obj; - v8_local_object_template(v8::Local o): obj(o) {}; + v8::Local obj; + v8_local_object_template(v8::Local o): obj(o) {}; }; struct v8_trycatch { - v8::TryCatch trycatch; - v8_trycatch(v8::Isolate *isolate): trycatch(isolate){} + v8::TryCatch trycatch; + v8_trycatch(v8::Isolate *isolate): trycatch(isolate){} }; struct v8_context_ref { - v8::Local context; - v8_context_ref(v8::Local ctx): context(ctx){} + v8::Local context; + v8_context_ref(v8::Local ctx): context(ctx){} }; struct v8_local_promise { - v8::Local promise; - v8_local_promise(v8::Local p): promise(p) {} + v8::Local promise; + v8_local_promise(v8::Local p): promise(p) {} }; struct v8_local_resolver { - v8::Local resolver; - v8_local_resolver(v8::Local r): resolver(r) {} + v8::Local resolver; + v8_local_resolver(v8::Local r): resolver(r) {} }; struct v8_local_object { - v8::Local obj; - v8_local_object(v8::Local o): obj(o) {} + v8::Local obj; + v8_local_object(v8::Local o): obj(o) {} }; struct v8_local_external_data { - v8::Local ext; - v8_local_external_data(v8::Local o): ext(o) {} + v8::Local ext; + v8_local_external_data(v8::Local o): ext(o) {} }; struct v8_local_set { - v8::Local set; - v8_local_set(v8::Local o): set(o) {} + v8::Local set; + v8_local_set(v8::Local o): set(o) {} }; struct v8_local_array { - v8::Local arr; - v8_local_array(v8::Local a): arr(a) {} + v8::Local arr; + v8_local_array(v8::Local a): arr(a) {} }; struct v8_local_array_buff { - v8::Local arr_buff; - v8_local_array_buff(v8::Local a): arr_buff(a) {} + v8::Local arr_buff; + v8_local_array_buff(v8::Local a): arr_buff(a) {} }; struct v8_embedded_data { - std::vector vec; - v8_embedded_data(): vec() {} - - void set(size_t index, void *d) { - vec.resize(index + 1); - vec[index] = d; - } - - void* get(size_t index) { - if (index >= vec.size()) { - return NULL; - } - return vec[index]; - } - - void reset(size_t index) { - if (index >= vec.size()) { - return; - } - vec[index] = NULL; - } + std::vector vec; + v8_embedded_data(): vec() {} + + void set(size_t index, void *d) { + vec.resize(index + 1); + vec[index] = d; + } + + void* get(size_t index) { + if (index >= vec.size()) { + return NULL; + } + return vec[index]; + } + + void reset(size_t index) { + if (index >= vec.size()) { + return; + } + vec[index] = NULL; + } }; typedef struct v8_native_function_pd v8_native_function_pd; @@ -189,1008 +206,1008 @@ typedef struct v8_pd_node v8_pd_node; typedef struct v8_pd_list v8_pd_list; struct v8_native_function_pd{ - v8_pd_node *node; - native_funcion func; - void *pd; - v8::Persistent *weak; - void(*freePD)(void *pd); + v8_pd_node *node; + native_funcion func; + void *pd; + v8::Persistent *weak; + void(*freePD)(void *pd); }; void v8_FreeNaticeFunctionPD(v8_native_function_pd *pd) { - pd->freePD(pd->pd); - pd->weak->Reset(); - delete pd->weak; - V8_FREE(pd); + pd->freePD(pd->pd); + pd->weak->Reset(); + delete pd->weak; + V8_FREE(pd); } struct v8_pd_node{ - v8_pd_list *list; - v8_pd_node *prev; - v8_pd_node *next; - void *data; - void (*free_data)(void *data); + v8_pd_list *list; + v8_pd_node *prev; + v8_pd_node *next; + void *data; + void (*free_data)(void *data); }; struct v8_pd_list{ - v8::ArrayBuffer::Allocator *allocator; - v8_pd_node *start; - v8_pd_node *end; + v8::ArrayBuffer::Allocator *allocator; + v8_pd_node *start; + v8_pd_node *end; }; void v8_ListNodeFree(v8_pd_node *node) { - if (node->free_data) { - node->free_data(node->data); - } - v8_pd_list *list = node->list; - if (list->start == node) { - list->start = node->next; - } - if (list->end == node) { - list->end = node->prev; - } - if (node->next) { - node->next->prev = node->prev; - } - if (node->prev) { - node->prev->next = node->next; - } - V8_FREE(node); + if (node->free_data) { + node->free_data(node->data); + } + v8_pd_list *list = node->list; + if (list->start == node) { + list->start = node->next; + } + if (list->end == node) { + list->end = node->prev; + } + if (node->next) { + node->next->prev = node->prev; + } + if (node->prev) { + node->prev->next = node->next; + } + V8_FREE(node); } v8_pd_node* v8_PDListAdd(v8_pd_list *list, void *pd, void (*free_data)(void *data)) { - v8_pd_node *new_node = (v8_pd_node*)V8_ALLOC(sizeof(*new_node)); - if (list->end) { - list->end->next = new_node; - } - new_node->list = list; - new_node->prev = list->end; - new_node->next = NULL; - new_node->data = pd; - new_node->free_data = free_data; - list->end = new_node; - if (!list->start) { - list->start = new_node; - } - - return new_node; + v8_pd_node *new_node = (v8_pd_node*)V8_ALLOC(sizeof(*new_node)); + if (list->end) { + list->end->next = new_node; + } + new_node->list = list; + new_node->prev = list->end; + new_node->next = NULL; + new_node->data = pd; + new_node->free_data = free_data; + list->end = new_node; + if (!list->start) { + list->start = new_node; + } + + return new_node; } void v8_PDListFree(v8_pd_list* pd_list) { - while (pd_list->end) { - v8_ListNodeFree(pd_list->end); - } - V8_FREE(pd_list); + while (pd_list->end) { + v8_ListNodeFree(pd_list->end); + } + V8_FREE(pd_list); } v8_pd_list* v8_PDListCreate(v8::ArrayBuffer::Allocator *alloc) { - v8_pd_list *native_data = (v8_pd_list*)V8_ALLOC(sizeof(*native_data)); - native_data->start = NULL; - native_data->end = NULL; - native_data->allocator = alloc; - return native_data; + v8_pd_list *native_data = (v8_pd_list*)V8_ALLOC(sizeof(*native_data)); + native_data->start = NULL; + native_data->end = NULL; + native_data->allocator = alloc; + return native_data; } int v8_InitializePlatform(int thread_pool_size, const char *flags) { - if (flags) { - v8::V8::SetFlagsFromString(flags); - } - if (strcmp(v8_Version(), V8_VERSION_STRING)) { - fprintf(stderr, "The library (%s) and the header versions (%s) mismatch.", v8_Version(), V8_VERSION_STRING); - return 0; - } - platform = v8::platform::NewDefaultPlatform(thread_pool_size).release(); - return 1; -} - -int v8_Initialize(v8_alloctor *alloc) { - v8::V8::InitializePlatform(platform); - v8::V8::Initialize(); - - if (alloc) { - allocator = alloc; - } else { - allocator = &DefaultAllocator; - } + if (flags) { + v8::V8::SetFlagsFromString(flags); + } + if (strcmp(v8_Version(), V8_VERSION_STRING)) { + fprintf(stderr, "The library (%s) and the header versions (%s) mismatch.\n", v8_Version(), V8_VERSION_STRING); + return 0; + } + GLOBAL_PLATFORM = v8::platform::NewDefaultPlatform(thread_pool_size).release(); + return 1; +} + +int v8_Initialize(v8_allocator *alloc) { + v8::V8::InitializePlatform(GLOBAL_PLATFORM); + v8::V8::Initialize(); + + if (alloc) { + allocator = alloc; + } else { + allocator = &DefaultAllocator; + } return 1; } const char* v8_Version() { - return v8::V8::GetVersion(); + return v8::V8::GetVersion(); } void v8_Dispose() { - v8::V8::Dispose(); - delete platform; + v8::V8::Dispose(); + delete GLOBAL_PLATFORM; } static void v8_FreeAllocator(v8::ArrayBuffer::Allocator* allocator) { - delete allocator; + delete allocator; } v8_isolate* v8_NewIsolate(size_t initial_heap_size_in_bytes, size_t maximum_heap_size_in_bytes) { - v8::Isolate::CreateParams create_params; - create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); - create_params.constraints.ConfigureDefaultsFromHeapSize(initial_heap_size_in_bytes, maximum_heap_size_in_bytes); - v8::Isolate *isolate = v8::Isolate::New(create_params); + v8::Isolate::CreateParams create_params; + create_params.array_buffer_allocator = v8::ArrayBuffer::Allocator::NewDefaultAllocator(); + create_params.constraints.ConfigureDefaultsFromHeapSize(initial_heap_size_in_bytes, maximum_heap_size_in_bytes); + v8::Isolate *isolate = v8::Isolate::New(create_params); - v8_pd_list *native_data = v8_PDListCreate(create_params.array_buffer_allocator); - isolate->SetData(0, native_data); + v8_pd_list *native_data = v8_PDListCreate(create_params.array_buffer_allocator); + isolate->SetData(OUR_SLOT, native_data); - return (v8_isolate*)isolate; + return (v8_isolate*)isolate; } void v8_IsolateSetFatalErrorHandler(v8_isolate* i, void (*fatal_hanlder)(const char* location, const char* message)) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->SetFatalErrorHandler(fatal_hanlder); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->SetFatalErrorHandler(fatal_hanlder); } void v8_IsolateSetOOMErrorHandler(v8_isolate* i, void (*oom_hanlder)(const char* location, int is_heap_oom)) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->SetOOMErrorHandler((v8::OOMErrorCallback)oom_hanlder); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->SetOOMErrorHandler((v8::OOMErrorCallback)oom_hanlder); } void v8_IsolateSetNearOOMHandler(v8_isolate* i, size_t (*near_oom_callback)(void* data, size_t current_heap_limit, size_t initial_heap_limit), void *pd, void(*free_pd)(void*)) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(0); - v8_PDListAdd(native_data, pd, free_pd); - isolate->AddNearHeapLimitCallback(near_oom_callback, pd); - isolate->AutomaticallyRestoreInitialHeapLimit(); + v8::Isolate *isolate = (v8::Isolate*)i; + v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(OUR_SLOT); + v8_PDListAdd(native_data, pd, free_pd); + isolate->AddNearHeapLimitCallback(near_oom_callback, pd); + isolate->AutomaticallyRestoreInitialHeapLimit(); } v8_isolate* v8_IsolateGetCurrent() { - return (v8_isolate*)v8::Isolate::GetCurrent(); + return (v8_isolate*)v8::Isolate::GetCurrent(); } void v8_RequestGCFromTesting(v8_isolate* i, int full) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->RequestGarbageCollectionForTesting(full? v8::Isolate::GarbageCollectionType::kFullGarbageCollection : v8::Isolate::GarbageCollectionType::kMinorGarbageCollection); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->RequestGarbageCollectionForTesting(full? v8::Isolate::GarbageCollectionType::kFullGarbageCollection : v8::Isolate::GarbageCollectionType::kMinorGarbageCollection); } size_t v8_IsolateUsedHeapSize(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::HeapStatistics heap; - isolate->GetHeapStatistics(&heap); - return heap.used_heap_size(); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::HeapStatistics heap; + isolate->GetHeapStatistics(&heap); + return heap.used_heap_size(); } size_t v8_IsolateTotalHeapSize(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::HeapStatistics heap; - isolate->GetHeapStatistics(&heap); - return heap.total_heap_size(); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::HeapStatistics heap; + isolate->GetHeapStatistics(&heap); + return heap.total_heap_size(); } size_t v8_IsolateHeapSizeLimit(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::HeapStatistics heap; - isolate->GetHeapStatistics(&heap); - return heap.heap_size_limit(); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::HeapStatistics heap; + isolate->GetHeapStatistics(&heap); + return heap.heap_size_limit(); } void v8_IsolateNotifyMemoryPressure(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->MemoryPressureNotification(v8::MemoryPressureLevel::kCritical); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->MemoryPressureNotification(v8::MemoryPressureLevel::kCritical); } void v8_TerminateCurrExecution(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->TerminateExecution(); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->TerminateExecution(); } void v8_CancelTerminateExecution(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->CancelTerminateExecution(); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->CancelTerminateExecution(); } void v8_FreeIsolate(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(0); - v8::ArrayBuffer::Allocator *allocator = native_data->allocator; - v8_PDListFree(native_data); - isolate->Dispose(); - v8_FreeAllocator(allocator); + v8::Isolate *isolate = (v8::Isolate*)i; + v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(OUR_SLOT); + v8::ArrayBuffer::Allocator *allocator = native_data->allocator; + v8_PDListFree(native_data); + isolate->Dispose(); + v8_FreeAllocator(allocator); } void v8_RequestInterrupt(v8_isolate* i, v8_InterruptCallback callback, void *data) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->RequestInterrupt((v8::InterruptCallback)callback, data); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->RequestInterrupt((v8::InterruptCallback)callback, data); } v8_isolate_scope* v8_IsolateEnter(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_isolate_scope *v8_isolateScope = (struct v8_isolate_scope*)V8_ALLOC(sizeof(*v8_isolateScope)); - v8_isolateScope = new(v8_isolateScope) v8_isolate_scope(isolate); - isolate->Enter(); - return v8_isolateScope; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_isolate_scope *v8_isolateScope = (struct v8_isolate_scope*)V8_ALLOC(sizeof(*v8_isolateScope)); + v8_isolateScope = new(v8_isolateScope) v8_isolate_scope(isolate); + isolate->Enter(); + return v8_isolateScope; } void v8_IsolateExit(v8_isolate_scope *v8_isolate_scope) { - v8_isolate_scope->isolate->Exit(); - v8_isolate_scope->~v8_isolate_scope(); - V8_FREE(v8_isolate_scope); + v8_isolate_scope->isolate->Exit(); + v8_isolate_scope->~v8_isolate_scope(); + V8_FREE(v8_isolate_scope); } void v8_IsolateRaiseException(v8_isolate *i, v8_local_value *exception) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->ThrowException(exception->val); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->ThrowException(exception->val); } v8_context_ref* v8_GetCurrentCtxRef(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_context_ref *ref = (v8_context_ref*) V8_ALLOC(sizeof(*ref)); - ref = new (ref) v8_context_ref(isolate->GetCurrentContext()); - return ref; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_context_ref *ref = (v8_context_ref*) V8_ALLOC(sizeof(*ref)); + ref = new (ref) v8_context_ref(isolate->GetCurrentContext()); + return ref; } void v8_IdleNotificationDeadline(v8_isolate *i, double deadline_in_seconds) { - v8::Isolate *isolate = (v8::Isolate*)i; - isolate->IdleNotificationDeadline(deadline_in_seconds); + v8::Isolate *isolate = (v8::Isolate*)i; + isolate->IdleNotificationDeadline(deadline_in_seconds); } v8_trycatch* v8_NewTryCatch(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_trycatch *trycatch = (v8_trycatch*) V8_ALLOC(sizeof(*trycatch)); - trycatch = new (trycatch) v8_trycatch(isolate); - return trycatch; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_trycatch *trycatch = (v8_trycatch*) V8_ALLOC(sizeof(*trycatch)); + trycatch = new (trycatch) v8_trycatch(isolate); + return trycatch; } v8_local_value* v8_TryCatchGetException(v8_trycatch *trycatch) { - v8::Local exception = trycatch->trycatch.Exception(); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(exception); - return v8_val; + v8::Local exception = trycatch->trycatch.Exception(); + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(exception); + return v8_val; } v8_local_value* v8_TryCatchGetTrace(v8_trycatch *trycatch, v8_context_ref* ctx) { - v8::MaybeLocal trace = trycatch->trycatch.StackTrace(ctx->context); - if (trace.IsEmpty()) { - return NULL; - } - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(trace.ToLocalChecked()); - return v8_val; + v8::MaybeLocal trace = trycatch->trycatch.StackTrace(ctx->context); + if (trace.IsEmpty()) { + return NULL; + } + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(trace.ToLocalChecked()); + return v8_val; } int v8_TryCatchHasTerminated(v8_trycatch *trycatch) { - return trycatch->trycatch.HasTerminated() ? 1 : 0; + return trycatch->trycatch.HasTerminated() ? 1 : 0; } void v8_FreeTryCatch(v8_trycatch *trycatch) { - trycatch->~v8_trycatch(); - V8_FREE(trycatch); + trycatch->~v8_trycatch(); + V8_FREE(trycatch); } v8_handlers_scope* v8_NewHandlersScope(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_handlers_scope *v8_handlersScope = (struct v8_handlers_scope*)V8_ALLOC(sizeof(*v8_handlersScope)); - v8_handlersScope = new (v8_handlersScope) v8_handlers_scope(isolate); - return v8_handlersScope; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_handlers_scope *v8_handlersScope = (struct v8_handlers_scope*)V8_ALLOC(sizeof(*v8_handlersScope)); + v8_handlersScope = new (v8_handlersScope) v8_handlers_scope(isolate); + return v8_handlersScope; } void v8_FreeHandlersScope(v8_handlers_scope* v8_handlersScope) { - v8_handlersScope->~v8_handlers_scope(); - V8_FREE(v8_handlersScope); + v8_handlersScope->~v8_handlers_scope(); + V8_FREE(v8_handlersScope); } static v8::Local v8_NewContexInternal(v8::Isolate* v8_isolate, v8_local_object_template *globals) { - if (globals) { - return v8::Context::New(v8_isolate, nullptr, globals->obj); - } else { - return v8::Context::New(v8_isolate); - } + if (globals) { + return v8::Context::New(v8_isolate, nullptr, globals->obj); + } else { + return v8::Context::New(v8_isolate); + } } v8_context* v8_NewContext(v8_isolate* i, v8_local_object_template *globals) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local context = v8_NewContexInternal(isolate, globals); - v8::Local data = v8::External::New(isolate, new v8_embedded_data()); - context->SetEmbedderData(DATA_INDEX(0), data); - v8::Persistent *persistent_ctx = new v8::Persistent(isolate, context); - v8_context *v8_context = (struct v8_context*)V8_ALLOC(sizeof(*v8_context)); - v8_context->persistent_ctx = persistent_ctx; - v8_context->isolate = isolate; - return v8_context; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local context = v8_NewContexInternal(isolate, globals); + v8::Local data = v8::External::New(isolate, new v8_embedded_data()); + context->SetEmbedderData(DATA_INDEX(0), data); + v8::Persistent *persistent_ctx = new v8::Persistent(isolate, context); + v8_context *v8_context = (struct v8_context*)V8_ALLOC(sizeof(*v8_context)); + v8_context->persistent_ctx = persistent_ctx; + v8_context->isolate = isolate; + return v8_context; } void v8_FreeContext(v8_context* ctx) { - v8::Isolate *isolate = ctx->isolate; - // in case the isolate are not entered we will enter it now, recursive enter is allow by the v8 - // so there is no harm in entering it again. - v8::Locker locker(isolate); - isolate->Enter(); - { - // We need this entire code to be in its own scope so the HandleScope will be freed before we exit the isolate. - - // we must create an handler scope to take a local reference to the context. - v8::HandleScope handler_scope(isolate); - v8::Local v8_ctx = ctx->persistent_ctx->Get(isolate); - - // Now we can take the embedder data and free it. - v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - delete embedded_data; - } + v8::Isolate *isolate = ctx->isolate; + // in case the isolate are not entered we will enter it now, recursive enter is allow by the v8 + // so there is no harm in entering it again. + v8::Locker locker(isolate); + isolate->Enter(); + { + // We need this entire code to be in its own scope so the HandleScope will be freed before we exit the isolate. + + // we must create an handler scope to take a local reference to the context. + v8::HandleScope handler_scope(isolate); + v8::Local v8_ctx = ctx->persistent_ctx->Get(isolate); + + // Now we can take the embedder data and free it. + v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + delete embedded_data; + } - ctx->persistent_ctx->Reset(); - delete ctx->persistent_ctx; - V8_FREE(ctx); + ctx->persistent_ctx->Reset(); + delete ctx->persistent_ctx; + V8_FREE(ctx); - isolate->Exit(); + isolate->Exit(); } void v8_SetPrivateData(v8_context* ctx, size_t index, void *pd) { - assert(pd); + assert(pd); - v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); + v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); - v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - embedded_data->set(index, pd); + v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + embedded_data->set(index, pd); } void v8_ResetPrivateData(v8_context *ctx, size_t index) { - v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); - v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - embedded_data->reset(index); + v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); + v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + embedded_data->reset(index); } void v8_ResetPrivateDataOnCtxRef(v8_context_ref* ctx_ref, size_t index) { - v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - embedded_data->reset(index); + v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + embedded_data->reset(index); } void* v8_GetPrivateData(v8_context* ctx, size_t index) { - v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); - v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - return embedded_data->get(index); + v8::Local v8_ctx = ctx->persistent_ctx->Get(ctx->isolate); + v8::Local data = v8::Local::Cast(v8_ctx->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + return embedded_data->get(index); } v8_context_ref* v8_ContextEnter(v8_context *v8_ctx) { - v8_context_ref *ref = (v8_context_ref*) V8_ALLOC(sizeof(*ref)); - ref = new (ref) v8_context_ref(v8_ctx->persistent_ctx->Get(v8_ctx->isolate)); - ref->context->Enter(); - return ref; + v8_context_ref *ref = (v8_context_ref*) V8_ALLOC(sizeof(*ref)); + ref = new (ref) v8_context_ref(v8_ctx->persistent_ctx->Get(v8_ctx->isolate)); + ref->context->Enter(); + return ref; } v8_isolate* v8_ContextRefGetIsolate(v8_context_ref *v8_ctx_ref) { - return (v8_isolate*)v8_ctx_ref->context->GetIsolate(); + return (v8_isolate*)v8_ctx_ref->context->GetIsolate(); } v8_local_object* v8_ContextRefGetGlobals(v8_context_ref *v8_ctx_ref) { - v8::Local globals = v8_ctx_ref->context->Global(); + v8::Local globals = v8_ctx_ref->context->Global(); - v8_local_object *v8_globals = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_globals)); - v8_globals = new (v8_globals) v8_local_object(globals); - return v8_globals; + v8_local_object *v8_globals = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_globals)); + v8_globals = new (v8_globals) v8_local_object(globals); + return v8_globals; } void v8_ExitContextRef(v8_context_ref *v8_ctx_ref) { - v8_ctx_ref->context->Exit(); + v8_ctx_ref->context->Exit(); } void v8_FreeContextRef(v8_context_ref *v8_ctx_ref) { - V8_FREE(v8_ctx_ref); + V8_FREE(v8_ctx_ref); } void* v8_GetPrivateDataFromCtxRef(v8_context_ref* ctx_ref, size_t index) { - v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - return embedded_data->get(index); + v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + return embedded_data->get(index); } void v8_SetPrivateDataOnCtxRef(v8_context_ref* ctx_ref, size_t index, void *pd) { - assert(pd); + assert(pd); - v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); - v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); - embedded_data->set(index, pd); + v8::Local data = v8::Local::Cast(ctx_ref->context->GetEmbedderData(DATA_INDEX(0))); + v8_embedded_data *embedded_data = (v8_embedded_data*)data->Value(); + embedded_data->set(index, pd); } v8_local_string* v8_NewString(v8_isolate* i, const char *str, size_t len) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_local_string *v8_str = (struct v8_local_string*)V8_ALLOC(sizeof(*v8_str)); - v8_str = new (v8_str) v8_local_string(isolate, str, len); - return v8_str; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_local_string *v8_str = (struct v8_local_string*)V8_ALLOC(sizeof(*v8_str)); + v8_str = new (v8_str) v8_local_string(isolate, str, len); + return v8_str; } v8_local_value* v8_StringToValue(v8_local_string *str) { - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(str->str); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(str->str); + return v8_val; } v8_local_object* v8_StringToStringObject(v8_isolate* i, v8_local_string *str) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local str_obj = v8::StringObject::New(isolate, str->str); - v8::Local obj = v8::Local::Cast(str_obj); - v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_object(obj); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local str_obj = v8::StringObject::New(isolate, str->str); + v8::Local obj = v8::Local::Cast(str_obj); + v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_object(obj); + return res; } void v8_FreeString(v8_local_string *str) { - V8_FREE(str); + V8_FREE(str); } static void v8_NativeBaseFunction(const v8::FunctionCallbackInfo& info) { - v8::Local data = v8::Handle::Cast(info.Data()); - v8_native_function_pd *nf_pd = (v8_native_function_pd*)data->Value(); - v8_local_value* val = nf_pd->func((v8_local_value_arr*)&info, info.Length(), nf_pd->pd); - if (val) { - info.GetReturnValue().Set(val->val); - V8_FREE(val); - } + v8::Local data = v8::Handle::Cast(info.Data()); + v8_native_function_pd *nf_pd = (v8_native_function_pd*)data->Value(); + v8_local_value* val = nf_pd->func((v8_local_value_arr*)&info, info.Length(), nf_pd->pd); + if (val) { + info.GetReturnValue().Set(val->val); + V8_FREE(val); + } } static void v8_FreeNativeFunctionPD(const v8::WeakCallbackInfo &data) { - v8_pd_node* node = data.GetParameter(); + v8_pd_node* node = data.GetParameter(); v8_ListNodeFree(node); } v8_local_native_function_template* v8_NewNativeFunctionTemplate(v8_isolate* i, native_funcion func, void *pd, void(*freePD)(void *pd)) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); - nf_pd->func = func; - nf_pd->pd = pd; - nf_pd->freePD = freePD; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); + nf_pd->func = func; + nf_pd->pd = pd; + nf_pd->freePD = freePD; - v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(0); - v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); + v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(OUR_SLOT); + v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); - v8::Local data = v8::External::New(isolate, (void*)nf_pd); - nf_pd->weak = new v8::Persistent(isolate, data); + v8::Local data = v8::External::New(isolate, (void*)nf_pd); + nf_pd->weak = new v8::Persistent(isolate, data); nf_pd->weak->SetWeak(node, v8_FreeNativeFunctionPD, v8::WeakCallbackType::kParameter); - v8::Local f = v8::FunctionTemplate::New(isolate, v8_NativeBaseFunction, data); - v8_local_native_function_template *v8_native = (struct v8_local_native_function_template*)V8_ALLOC(sizeof(*v8_native)); - v8_native = new (v8_native) v8_local_native_function_template(f); - return v8_native; + v8::Local f = v8::FunctionTemplate::New(isolate, v8_NativeBaseFunction, data); + v8_local_native_function_template *v8_native = (struct v8_local_native_function_template*)V8_ALLOC(sizeof(*v8_native)); + v8_native = new (v8_native) v8_local_native_function_template(f); + return v8_native; } v8_local_native_function* v8_NativeFunctionTemplateToFunction(v8_context_ref *ctx_ref, v8_local_native_function_template *func) { - v8::Local f = func->func->GetFunction(ctx_ref->context).ToLocalChecked(); - v8_local_native_function *ret = (v8_local_native_function*) V8_ALLOC(sizeof(*ret)); - ret = new (ret) v8_local_native_function(f); - return ret; + v8::Local f = func->func->GetFunction(ctx_ref->context).ToLocalChecked(); + v8_local_native_function *ret = (v8_local_native_function*) V8_ALLOC(sizeof(*ret)); + ret = new (ret) v8_local_native_function(f); + return ret; } v8_local_native_function* v8_NewNativeFunction(v8_context_ref *ctx_ref, native_funcion func, void *pd, void(*freePD)(void *pd)) { - v8::Isolate *isolate = ctx_ref->context->GetIsolate(); - v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); - nf_pd->func = func; - nf_pd->pd = pd; - nf_pd->freePD = freePD; + v8::Isolate *isolate = ctx_ref->context->GetIsolate(); + v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); + nf_pd->func = func; + nf_pd->pd = pd; + nf_pd->freePD = freePD; - v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(0); - v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); + v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(OUR_SLOT); + v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); - v8::Local data = v8::External::New(ctx_ref->context->GetIsolate(), (void*)nf_pd); - nf_pd->weak = new v8::Persistent(isolate, data); - nf_pd->weak->SetWeak(node, v8_FreeNativeFunctionPD, v8::WeakCallbackType::kParameter); + v8::Local data = v8::External::New(ctx_ref->context->GetIsolate(), (void*)nf_pd); + nf_pd->weak = new v8::Persistent(isolate, data); + nf_pd->weak->SetWeak(node, v8_FreeNativeFunctionPD, v8::WeakCallbackType::kParameter); - v8::Local f = v8::Function::New(ctx_ref->context, v8_NativeBaseFunction, data).ToLocalChecked(); + v8::Local f = v8::Function::New(ctx_ref->context, v8_NativeBaseFunction, data).ToLocalChecked(); - v8_local_native_function *ret = (v8_local_native_function*) V8_ALLOC(sizeof(*ret)); - ret = new (ret) v8_local_native_function(f); - return ret; + v8_local_native_function *ret = (v8_local_native_function*) V8_ALLOC(sizeof(*ret)); + ret = new (ret) v8_local_native_function(f); + return ret; } void v8_FreeNativeFunctionTemplate(v8_local_native_function_template *func) { - V8_FREE(func); + V8_FREE(func); } void v8_FreeNativeFunction(v8_local_native_function *func) { - V8_FREE(func); + V8_FREE(func); } v8_local_value* v8_NativeFunctionToValue(v8_local_native_function *func) { - v8::Local v = v8::Local::Cast(func->func); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(v); - return v8_val; + v8::Local v = v8::Local::Cast(func->func); + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(v); + return v8_val; } v8_local_value* v8_ArgsGet(v8_local_value_arr *args, size_t i) { - v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; - v8::Handle v = (*info)[i]; - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(v); - return v8_val; + v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; + v8::Handle v = (*info)[i]; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(v); + return v8_val; } v8_local_object* v8_ArgsGetSelf(v8_local_value_arr *args) { - v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; - v8::Local holder = info->Holder(); - v8_local_object *v8_obj = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_obj)); - v8_obj = new (v8_obj) v8_local_object(holder); - return v8_obj; + v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; + v8::Local holder = info->Holder(); + v8_local_object *v8_obj = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_obj)); + v8_obj = new (v8_obj) v8_local_object(holder); + return v8_obj; } v8_isolate* v8_GetCurrentIsolate(v8_local_value_arr *args) { - v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; - v8::Isolate* isolate = info->GetIsolate(); - return (v8_isolate*)isolate; + v8::FunctionCallbackInfo *info = (v8::FunctionCallbackInfo *)args; + v8::Isolate* isolate = info->GetIsolate(); + return (v8_isolate*)isolate; } v8_local_object_template* v8_NewObjectTemplate(v8_isolate* i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local obj = v8::ObjectTemplate::New(isolate); - v8_local_object_template *v8_obj = (struct v8_local_object_template*)V8_ALLOC(sizeof(*v8_obj)); - v8_obj = new (v8_obj) v8_local_object_template(obj); - return v8_obj; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local obj = v8::ObjectTemplate::New(isolate); + v8_local_object_template *v8_obj = (struct v8_local_object_template*)V8_ALLOC(sizeof(*v8_obj)); + v8_obj = new (v8_obj) v8_local_object_template(obj); + return v8_obj; } void v8_FreeObjectTemplate(v8_local_object_template *obj) { - V8_FREE(obj); + V8_FREE(obj); } void v8_ObjectTemplateSetFunction(v8_local_object_template *obj, v8_local_string *name, v8_local_native_function_template *f) { - obj->obj->Set(name->str, f->func); + obj->obj->Set(name->str, f->func); } void v8_ObjectTemplateSetObject(v8_local_object_template *obj, v8_local_string *name, v8_local_object_template *o) { - obj->obj->Set(name->str, o->obj); + obj->obj->Set(name->str, o->obj); } void v8_ObjectTemplateSetValue(v8_local_object_template *obj, v8_local_string *name, v8_local_value *val) { - obj->obj->Set(name->str, val->val); + obj->obj->Set(name->str, val->val); } void v8_ObjectTemplateSetInternalFieldCount(v8_local_object_template *obj, size_t count) { - obj->obj->SetInternalFieldCount(count); + obj->obj->SetInternalFieldCount(count); } v8_local_object* v8_ObjectTemplateNewInstance(v8_context_ref *ctx_ref, v8_local_object_template *obj) { - v8::Local v = obj->obj->NewInstance(ctx_ref->context).ToLocalChecked(); - v8_local_object *v8_val = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_object(v); - return v8_val; + v8::Local v = obj->obj->NewInstance(ctx_ref->context).ToLocalChecked(); + v8_local_object *v8_val = (struct v8_local_object*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_object(v); + return v8_val; } v8_local_script* v8_Compile(v8_context_ref* v8_ctx_ref, v8_local_string* str) { - v8_local_script *v8_script = (struct v8_local_script*)V8_ALLOC(sizeof(*v8_script)); - v8_script = new (v8_script) v8_local_script(v8_ctx_ref->context, str); - if (v8_script->script.IsEmpty()) { - V8_FREE(v8_script); - return NULL; - } - return v8_script; + v8_local_script *v8_script = (struct v8_local_script*)V8_ALLOC(sizeof(*v8_script)); + v8_script = new (v8_script) v8_local_script(v8_ctx_ref->context, str); + if (v8_script->script.IsEmpty()) { + V8_FREE(v8_script); + return NULL; + } + return v8_script; } v8_persisted_object_template* v8_ObjectTemplatePersist(v8_isolate *i, v8_local_object_template *obj) { - v8::Isolate *isolate = (v8::Isolate*)i; - return (v8_persisted_object_template*) new v8::Persistent(isolate, obj->obj); + v8::Isolate *isolate = (v8::Isolate*)i; + return (v8_persisted_object_template*) new v8::Persistent(isolate, obj->obj); } v8_persisted_script* v8_ScriptPersist(v8_isolate *i, v8_local_script* script) { - v8::Isolate *isolate = (v8::Isolate*)i; - return (v8_persisted_script*) new v8::Persistent(isolate, script->script); + v8::Isolate *isolate = (v8::Isolate*)i; + return (v8_persisted_script*) new v8::Persistent(isolate, script->script); } v8_local_script* v8_PersistedScriptToLocal(v8_isolate *i, v8_persisted_script* script) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Persistent *persisted_script = (v8::Persistent*)script; - v8::Local s = v8::Local::New(isolate, *persisted_script); - v8_local_script *local_script = (struct v8_local_script*)V8_ALLOC(sizeof(*local_script)); - local_script = new (local_script) v8_local_script(s); - return local_script; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Persistent *persisted_script = (v8::Persistent*)script; + v8::Local s = v8::Local::New(isolate, *persisted_script); + v8_local_script *local_script = (struct v8_local_script*)V8_ALLOC(sizeof(*local_script)); + local_script = new (local_script) v8_local_script(s); + return local_script; } v8_local_object_template* v8_PersistedObjectTemplateToLocal(v8_isolate *i, v8_persisted_object_template* obj) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Persistent *persisted_obj = (v8::Persistent*)obj; - v8::Local o = v8::Local::New(isolate, *persisted_obj); - v8_local_object_template *local_obj = (struct v8_local_object_template*)V8_ALLOC(sizeof(*local_obj)); - local_obj = new (local_obj) v8_local_object_template(o); - return local_obj; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Persistent *persisted_obj = (v8::Persistent*)obj; + v8::Local o = v8::Local::New(isolate, *persisted_obj); + v8_local_object_template *local_obj = (struct v8_local_object_template*)V8_ALLOC(sizeof(*local_obj)); + local_obj = new (local_obj) v8_local_object_template(o); + return local_obj; } void v8_FreePersistedScript(v8_persisted_script* script) { - v8::Persistent *persisted_script = (v8::Persistent*)script; - persisted_script->Reset(); - delete persisted_script; + v8::Persistent *persisted_script = (v8::Persistent*)script; + persisted_script->Reset(); + delete persisted_script; } void v8_FreePersistedObjectTemplate(v8_persisted_object_template* obj) { - v8::Persistent *persisted_script = (v8::Persistent*)obj; - persisted_script->Reset(); - delete persisted_script; + v8::Persistent *persisted_script = (v8::Persistent*)obj; + persisted_script->Reset(); + delete persisted_script; } static v8::MaybeLocal v8_ResolveModules(v8::Local context, v8::Local specifier, - v8::Local import_assertions, v8::Local referrer) { - v8::Local external = v8::Local::Cast(context->GetEmbedderData(1)); - V8_LoadModuleCallback load_module_callback = (V8_LoadModuleCallback)external->Value(); + v8::Local import_assertions, v8::Local referrer) { + v8::Local external = v8::Local::Cast(context->GetEmbedderData(1)); + V8_LoadModuleCallback load_module_callback = (V8_LoadModuleCallback)external->Value(); - v8_context_ref *v8_ctx_ref = (struct v8_context_ref*)V8_ALLOC(sizeof(*v8_ctx_ref)); - v8_ctx_ref = new (v8_ctx_ref) v8_context_ref(context); + v8_context_ref *v8_ctx_ref = (struct v8_context_ref*)V8_ALLOC(sizeof(*v8_ctx_ref)); + v8_ctx_ref = new (v8_ctx_ref) v8_context_ref(context); - v8_local_string* name = (struct v8_local_string*)V8_ALLOC(sizeof(*name)); - int identity_hash = referrer->GetIdentityHash(); + v8_local_string* name = (struct v8_local_string*)V8_ALLOC(sizeof(*name)); + int identity_hash = referrer->GetIdentityHash(); - name = new (name) v8_local_string(specifier); + name = new (name) v8_local_string(specifier); - v8_local_module* m = load_module_callback(v8_ctx_ref, name, identity_hash); + v8_local_module* m = load_module_callback(v8_ctx_ref, name, identity_hash); - v8::MaybeLocal res; - if (m) { - res = m->mod; - v8_FreeModule(m); - } + v8::MaybeLocal res; + if (m) { + res = m->mod; + v8_FreeModule(m); + } - return res; + return res; } v8_local_module* v8_CompileAsModule(v8_context_ref* v8_ctx_ref, v8_local_string* name, v8_local_string* code, int is_module) { - v8::Isolate *isolate = v8_ctx_ref->context->GetIsolate(); - v8::ScriptOrigin origin(isolate, name->str, 0, 0, false, -1, v8::Local(), false, false, is_module, v8::Local()); + v8::Isolate *isolate = v8_ctx_ref->context->GetIsolate(); + v8::ScriptOrigin origin(isolate, name->str, 0, 0, false, -1, v8::Local(), false, false, is_module, v8::Local()); - v8::ScriptCompiler::Source source(code->str, origin); - v8::MaybeLocal mod = v8::ScriptCompiler::CompileModule(isolate, &source); + v8::ScriptCompiler::Source source(code->str, origin); + v8::MaybeLocal mod = v8::ScriptCompiler::CompileModule(isolate, &source); - if (mod.IsEmpty()) { - return NULL; - } + if (mod.IsEmpty()) { + return NULL; + } - v8_local_module *ret = (struct v8_local_module*)V8_ALLOC(sizeof(*ret)); - ret = new (ret) v8_local_module(mod.ToLocalChecked()); - return ret; + v8_local_module *ret = (struct v8_local_module*)V8_ALLOC(sizeof(*ret)); + ret = new (ret) v8_local_module(mod.ToLocalChecked()); + return ret; } int v8_InitiateModule(v8_local_module* m, v8_context_ref* v8_ctx_ref, V8_LoadModuleCallback load_module_callback) { - assert(load_module_callback); + assert(load_module_callback); - v8::Isolate *isolate = v8_ctx_ref->context->GetIsolate(); - v8::Local data = v8::External::New(isolate, (void*)load_module_callback); - v8_ctx_ref->context->SetEmbedderData(1, data); - v8::Maybe res = m->mod->InstantiateModule(v8_ctx_ref->context, v8_ResolveModules); - return res.IsNothing() ? 0 : 1; + v8::Isolate *isolate = v8_ctx_ref->context->GetIsolate(); + v8::Local data = v8::External::New(isolate, (void*)load_module_callback); + v8_ctx_ref->context->SetEmbedderData(1, data); + v8::Maybe res = m->mod->InstantiateModule(v8_ctx_ref->context, v8_ResolveModules); + return res.IsNothing() ? 0 : 1; } int v8_ModuleGetIdentityHash(v8_local_module* m) { - return m->mod->GetIdentityHash(); + return m->mod->GetIdentityHash(); } v8_local_value* v8_EvaluateModule(v8_local_module* m, v8_context_ref* v8_ctx_ref) { - v8::MaybeLocal res = m->mod->Evaluate(v8_ctx_ref->context); - if (res.IsEmpty()) { - return NULL; - } + v8::MaybeLocal res = m->mod->Evaluate(v8_ctx_ref->context); + if (res.IsEmpty()) { + return NULL; + } - v8_local_value *val = (struct v8_local_value*)V8_ALLOC(sizeof(*val)); - val = new (val) v8_local_value(res.ToLocalChecked()); - return val; + v8_local_value *val = (struct v8_local_value*)V8_ALLOC(sizeof(*val)); + val = new (val) v8_local_value(res.ToLocalChecked()); + return val; } v8_persisted_module* v8_ModulePersist(v8_isolate *i, v8_local_module* m) { - v8::Isolate *isolate = (v8::Isolate*)i; - return (v8_persisted_module*) new v8::Persistent(isolate, m->mod); + v8::Isolate *isolate = (v8::Isolate*)i; + return (v8_persisted_module*) new v8::Persistent(isolate, m->mod); } v8_local_module* v8_ModuleToLocal(v8_isolate *i, v8_persisted_module* m) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Persistent *persisted_module = (v8::Persistent*)m; - v8_local_module *local_module = (struct v8_local_module*)V8_ALLOC(sizeof(*local_module)); - local_module = new (local_module) v8_local_module(isolate, persisted_module); - return local_module; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Persistent *persisted_module = (v8::Persistent*)m; + v8_local_module *local_module = (struct v8_local_module*)V8_ALLOC(sizeof(*local_module)); + local_module = new (local_module) v8_local_module(isolate, persisted_module); + return local_module; } void v8_FreePersistedModule(v8_persisted_module* m) { - v8::Persistent *persisted_module = (v8::Persistent*)m; - persisted_module->Reset(); - delete persisted_module; + v8::Persistent *persisted_module = (v8::Persistent*)m; + persisted_module->Reset(); + delete persisted_module; } void v8_FreeModule(v8_local_module* m) { - V8_FREE(m); + V8_FREE(m); } void v8_FreeScript(v8_local_script *script) { - V8_FREE(script); + V8_FREE(script); } v8_local_value* v8_Run(v8_context_ref* v8_ctx_ref, v8_local_script* script) { - v8::MaybeLocal result = script->script->Run(v8_ctx_ref->context); - if (result.IsEmpty()) { - return NULL; - } + v8::MaybeLocal result = script->script->Run(v8_ctx_ref->context); + if (result.IsEmpty()) { + return NULL; + } - v8::Local res = result.ToLocalChecked(); + v8::Local res = result.ToLocalChecked(); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(res); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(res); + return v8_val; } int v8_ValueIsFunction(v8_local_value *val){ - return val->val->IsFunction(); + return val->val->IsFunction(); } v8_local_value* v8_FunctionCall(v8_context_ref *v8_ctx_ref, v8_local_value *val, size_t argc, v8_local_value* const* argv) { - v8::Local argv_arr[argc]; - for (size_t i = 0 ; i < argc ; ++i) { - argv_arr[i] = argv[i]->val; - } - v8::Local function = v8::Local::Cast(val->val); - v8::MaybeLocal result = function->Call(v8_ctx_ref->context, v8_ctx_ref->context->Global(), argc, argv_arr); - if (result.IsEmpty()) { - return NULL; - } + v8::Local argv_arr[argc]; + for (size_t i = 0 ; i < argc ; ++i) { + argv_arr[i] = argv[i]->val; + } + v8::Local function = v8::Local::Cast(val->val); + v8::MaybeLocal result = function->Call(v8_ctx_ref->context, v8_ctx_ref->context->Global(), argc, argv_arr); + if (result.IsEmpty()) { + return NULL; + } - v8::Local res = result.ToLocalChecked(); + v8::Local res = result.ToLocalChecked(); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(res); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(res); + return v8_val; } int v8_ValueIsAsyncFunction(v8_local_value *val) { - return val->val->IsAsyncFunction(); + return val->val->IsAsyncFunction(); } int v8_ValueIsString(v8_local_value *val) { - return val->val->IsString(); + return val->val->IsString(); } int v8_ValueIsStringObject(v8_local_value *val) { - return val->val->IsStringObject(); + return val->val->IsStringObject(); } v8_local_string* v8_ValueAsString(v8_local_value *val) { - v8_local_string *v8_str = (struct v8_local_string*)V8_ALLOC(sizeof(*v8_str)); - v8_str = new (v8_str) v8_local_string(v8::Local::Cast(val->val)); - return v8_str; + v8_local_string *v8_str = (struct v8_local_string*)V8_ALLOC(sizeof(*v8_str)); + v8_str = new (v8_str) v8_local_string(v8::Local::Cast(val->val)); + return v8_str; } v8_local_value* v8_ValueFromLong(v8_isolate *i, long long val) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local big_int = v8::BigInt::New(isolate, val); - v8::Local v = v8::Local::Cast(big_int); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local big_int = v8::BigInt::New(isolate, val); + v8::Local v = v8::Local::Cast(big_int); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(v); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(v); + return v8_val; } int v8_ValueIsBigInt(v8_local_value *val) { - return val->val->IsBigInt() || val->val->IsInt32(); + return val->val->IsBigInt() || val->val->IsInt32(); } long long v8_GetBigInt(v8_local_value *val) { - if (val->val->IsInt32()) { - v8::Local integer = v8::Local::Cast(val->val); - int64_t res = integer->Value(); - return res; - } - v8::Local big_int = v8::Local::Cast(val->val); - int64_t res = big_int->Int64Value(); - return res; + if (val->val->IsInt32()) { + v8::Local integer = v8::Local::Cast(val->val); + int64_t res = integer->Value(); + return res; + } + v8::Local big_int = v8::Local::Cast(val->val); + int64_t res = big_int->Int64Value(); + return res; } int v8_ValueIsNumber(v8_local_value *val) { - return val->val->IsNumber(); + return val->val->IsNumber(); } double v8_GetNumber(v8_local_value *val) { - v8::Local number = v8::Local::Cast(val->val); - return number->Value(); + v8::Local number = v8::Local::Cast(val->val); + return number->Value(); } int v8_ValueIsBool(v8_local_value *val) { - return val->val->IsBoolean(); + return val->val->IsBoolean(); } int v8_GetBool(v8_local_value *val){ - v8::Local boolean = v8::Local::Cast(val->val); - return boolean->Value(); + v8::Local boolean = v8::Local::Cast(val->val); + return boolean->Value(); } v8_local_value* v8_ValueFromDouble(v8_isolate *i, double val) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local number = v8::Number::New(isolate, val); - v8::Local v = v8::Local::Cast(number); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local number = v8::Number::New(isolate, val); + v8::Local v = v8::Local::Cast(number); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(v); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(v); + return v8_val; } int v8_ValueIsPromise(v8_local_value *val) { - return val->val->IsPromise(); + return val->val->IsPromise(); } v8_local_promise* v8_ValueAsPromise(v8_local_value *val) { - v8::Local promise = v8::Local::Cast(val->val); - v8_local_promise* p = (v8_local_promise*)V8_ALLOC(sizeof(*p)); - p = new (p) v8_local_promise(promise); - return p; + v8::Local promise = v8::Local::Cast(val->val); + v8_local_promise* p = (v8_local_promise*)V8_ALLOC(sizeof(*p)); + p = new (p) v8_local_promise(promise); + return p; } void v8_FreePromise(v8_local_promise* promise) { - V8_FREE(promise); + V8_FREE(promise); } v8_PromiseState v8_PromiseGetState(v8_local_promise* promise) { - v8::Promise::PromiseState s = promise->promise->State(); - switch(s) { - case v8::Promise::PromiseState::kPending: - return v8_PromiseState_Pending; - case v8::Promise::PromiseState::kFulfilled: - return v8_PromiseState_Fulfilled; - case v8::Promise::PromiseState::kRejected: - return v8_PromiseState_Rejected; - } - return v8_PromiseState_Unknown; + v8::Promise::PromiseState s = promise->promise->State(); + switch(s) { + case v8::Promise::PromiseState::kPending: + return v8_PromiseState_Pending; + case v8::Promise::PromiseState::kFulfilled: + return v8_PromiseState_Fulfilled; + case v8::Promise::PromiseState::kRejected: + return v8_PromiseState_Rejected; + } + return v8_PromiseState_Unknown; } v8_local_value* v8_PromiseGetResult(v8_local_promise* promise) { - v8_local_value* val = (v8_local_value*)V8_ALLOC(sizeof(*val)); - val = new (val) v8_local_value(promise->promise->Result()); - return val; + v8_local_value* val = (v8_local_value*)V8_ALLOC(sizeof(*val)); + val = new (val) v8_local_value(promise->promise->Result()); + return val; } void v8_PromiseThen(v8_local_promise* promise, v8_context_ref *ctx_ref, v8_local_native_function *resolve, v8_local_native_function *reject) { - v8::MaybeLocal _may_local = promise->promise->Then(ctx_ref->context, resolve->func, reject->func); + v8::MaybeLocal _may_local = promise->promise->Then(ctx_ref->context, resolve->func, reject->func); } typedef void (*OnFreed)(void *); typedef struct ValueFreedCtx { - OnFreed on_freed; - void *pd; - v8::Persistent *weak; + OnFreed on_freed; + void *pd; + v8::Persistent *weak; } ValueFreedCtx ; static void v8_ValueOnFreedCallback(const v8::WeakCallbackInfo &data) { - ValueFreedCtx* free_ctx = data.GetParameter(); - free_ctx->on_freed(free_ctx->pd); - free_ctx->weak->Reset(); - delete free_ctx->weak; - V8_FREE(free_ctx); + ValueFreedCtx* free_ctx = data.GetParameter(); + free_ctx->on_freed(free_ctx->pd); + free_ctx->weak->Reset(); + delete free_ctx->weak; + V8_FREE(free_ctx); } void v8_ValueOnFreed(v8_local_value* value, v8_isolate *i, OnFreed on_freed, void *pd) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Persistent *persist = new v8::Persistent(isolate, value->val); - ValueFreedCtx *free_ctx = (ValueFreedCtx*)V8_ALLOC(sizeof(*free_ctx)); - free_ctx->on_freed = on_freed; - free_ctx->pd = pd; - free_ctx->weak = persist; - persist->SetWeak(free_ctx, v8_ValueOnFreedCallback, v8::WeakCallbackType::kParameter); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Persistent *persist = new v8::Persistent(isolate, value->val); + ValueFreedCtx *free_ctx = (ValueFreedCtx*)V8_ALLOC(sizeof(*free_ctx)); + free_ctx->on_freed = on_freed; + free_ctx->pd = pd; + free_ctx->weak = persist; + persist->SetWeak(free_ctx, v8_ValueOnFreedCallback, v8::WeakCallbackType::kParameter); } v8_local_value* v8_PromiseToValue(v8_local_promise *promise) { - v8::Local val = v8::Local::Cast(promise->promise); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(promise->promise); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_resolver* v8_NewResolver(v8_context_ref *ctx_ref) { - v8::Local resolver = v8::Promise::Resolver::New(ctx_ref->context).ToLocalChecked(); - v8_local_resolver *res = (v8_local_resolver*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_resolver(resolver); - return res; + v8::Local resolver = v8::Promise::Resolver::New(ctx_ref->context).ToLocalChecked(); + v8_local_resolver *res = (v8_local_resolver*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_resolver(resolver); + return res; } void v8_FreeResolver(v8_local_resolver *resolver) { - V8_FREE(resolver); + V8_FREE(resolver); } v8_local_promise* v8_ResolverGetPromise(v8_local_resolver *resolver) { - v8::Local promise = resolver->resolver->GetPromise(); - v8_local_promise *res = (v8_local_promise*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_promise(promise); - return res; + v8::Local promise = resolver->resolver->GetPromise(); + v8_local_promise *res = (v8_local_promise*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_promise(promise); + return res; } void v8_ResolverResolve(v8_context_ref *ctx_ref, v8_local_resolver *resolver, v8_local_value *val) { - v8::Maybe res = resolver->resolver->Resolve(ctx_ref->context, val->val); + v8::Maybe res = resolver->resolver->Resolve(ctx_ref->context, val->val); } void v8_ResolverReject(v8_context_ref *ctx_ref, v8_local_resolver *resolver, v8_local_value *val) { - v8::Maybe res = resolver->resolver->Reject(ctx_ref->context, val->val); + v8::Maybe res = resolver->resolver->Reject(ctx_ref->context, val->val); } v8_local_value* v8_ResolverToValue(v8_local_resolver *resolver) { - v8::Local val = v8::Local::Cast(resolver->resolver); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(resolver->resolver); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } int v8_ValueIsObject(v8_local_value *val) { - return val->val->IsObject(); + return val->val->IsObject(); } int v8_ValueIsExternalData(v8_local_value *val) { - return val->val->IsExternal(); + return val->val->IsExternal(); } v8_local_array* v8_ValueGetPropertyNames(v8_context_ref *ctx_ref, v8_local_object *obj) { - v8::MaybeLocal maybe_res = obj->obj->GetPropertyNames(ctx_ref->context); - if (maybe_res.IsEmpty()) { - return NULL; - } - v8::Local arr = maybe_res.ToLocalChecked(); - v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array(arr); - return res; + v8::MaybeLocal maybe_res = obj->obj->GetPropertyNames(ctx_ref->context); + if (maybe_res.IsEmpty()) { + return NULL; + } + v8::Local arr = maybe_res.ToLocalChecked(); + v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array(arr); + return res; } v8_local_array* v8_ValueGetOwnPropertyNames(v8_context_ref *ctx_ref, v8_local_object *obj) { - v8::MaybeLocal maybe_res = obj->obj->GetOwnPropertyNames(ctx_ref->context, v8::PropertyFilter::ALL_PROPERTIES); - if (maybe_res.IsEmpty()) { - return NULL; - } - v8::Local arr = maybe_res.ToLocalChecked(); - v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array(arr); - return res; + v8::MaybeLocal maybe_res = obj->obj->GetOwnPropertyNames(ctx_ref->context, v8::PropertyFilter::ALL_PROPERTIES); + if (maybe_res.IsEmpty()) { + return NULL; + } + v8::Local arr = maybe_res.ToLocalChecked(); + v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array(arr); + return res; } int v8_DeletePropery(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key) { - v8::Maybe res = obj->obj->Delete(ctx_ref->context, key->val); - if (res.IsNothing()) { - return false; - } - return res.ToChecked(); + v8::Maybe res = obj->obj->Delete(ctx_ref->context, key->val); + if (res.IsNothing()) { + return false; + } + return res.ToChecked(); } int v8_ValueIsArray(v8_local_value *val) { - return val->val->IsArray(); + return val->val->IsArray(); } int v8_ValueIsArrayBuffer(v8_local_value *val) { - return val->val->IsArrayBuffer(); + return val->val->IsArrayBuffer(); } v8_local_object* v8_NewObject(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local obj = v8::Object::New(isolate); - v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_object(obj); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local obj = v8::Object::New(isolate); + v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_object(obj); + return res; } v8_local_external_data* v8_NewExternalData(v8_isolate *i, void *data, void(*free)(void*)) { - v8::Isolate *isolate = (v8::Isolate*)i; + v8::Isolate *isolate = (v8::Isolate*)i; - // abusing native function infra - v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); - nf_pd->func = NULL; - nf_pd->pd = data; - nf_pd->freePD = free; + // abusing native function infra + v8_native_function_pd *nf_pd = (v8_native_function_pd*)V8_ALLOC(sizeof(*nf_pd)); + nf_pd->func = NULL; + nf_pd->pd = data; + nf_pd->freePD = free; - v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(0); - v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); + v8_pd_list *native_data = (v8_pd_list*)isolate->GetData(OUR_SLOT); + v8_pd_node* node = v8_PDListAdd(native_data, (void*)nf_pd, (void(*)(void*))v8_FreeNaticeFunctionPD); - v8::Local d = v8::External::New(isolate, (void*)nf_pd); - nf_pd->weak = new v8::Persistent(isolate, d); - nf_pd->weak->SetWeak(node, v8_FreeNativeFunctionPD, v8::WeakCallbackType::kParameter); + v8::Local d = v8::External::New(isolate, (void*)nf_pd); + nf_pd->weak = new v8::Persistent(isolate, d); + nf_pd->weak->SetWeak(node, v8_FreeNativeFunctionPD, v8::WeakCallbackType::kParameter); - v8_local_external_data *res = (v8_local_external_data*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_external_data(d); - return res; + v8_local_external_data *res = (v8_local_external_data*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_external_data(d); + return res; } void* v8_ExternalDataGet(v8_local_external_data *ext) { - return ((v8_native_function_pd *)ext->ext->Value())->pd; + return ((v8_native_function_pd *)ext->ext->Value())->pd; } v8_local_value* v8_NewObjectFromJsonString(v8_context_ref *ctx_ref, v8_local_string *str) { - v8::MaybeLocal result = v8::JSON::Parse(ctx_ref->context, str->str); - if (result.IsEmpty()) { - return NULL; - } - v8::Local val = result.ToLocalChecked(); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::MaybeLocal result = v8::JSON::Parse(ctx_ref->context, str->str); + if (result.IsEmpty()) { + return NULL; + } + v8::Local val = result.ToLocalChecked(); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_string* v8_JsonStringify(v8_context_ref *ctx_ref, v8_local_value *val) { @@ -1204,288 +1221,288 @@ v8_local_string* v8_JsonStringify(v8_context_ref *ctx_ref, v8_local_value *val) } v8_local_object* v8_ValueAsObject(v8_local_value *val) { - v8::Local obj = v8::Local::Cast(val->val); - v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_object(obj); - return res; + v8::Local obj = v8::Local::Cast(val->val); + v8_local_object *res = (v8_local_object*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_object(obj); + return res; } v8_local_external_data* v8_ValueAsExternalData(v8_local_value *val) { - v8::Local ext = v8::Local::Cast(val->val); - v8_local_external_data *res = (v8_local_external_data*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_external_data(ext); - return res; + v8::Local ext = v8::Local::Cast(val->val); + v8_local_external_data *res = (v8_local_external_data*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_external_data(ext); + return res; } v8_local_resolver* v8_ValueAsResolver(v8_local_value *val) { - v8::Local resolver = v8::Local::Cast(val->val); - v8_local_resolver *res = (v8_local_resolver*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_resolver(resolver); - return res; + v8::Local resolver = v8::Local::Cast(val->val); + v8_local_resolver *res = (v8_local_resolver*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_resolver(resolver); + return res; } v8_local_value* v8_ObjectGet(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key) { - v8::MaybeLocal maybe_val = obj->obj->Get(ctx_ref->context, key->val); - if (maybe_val.IsEmpty()) { - return NULL; - } - v8::Local val = maybe_val.ToLocalChecked(); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::MaybeLocal maybe_val = obj->obj->Get(ctx_ref->context, key->val); + if (maybe_val.IsEmpty()) { + return NULL; + } + v8::Local val = maybe_val.ToLocalChecked(); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } void v8_ObjectSet(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key, v8_local_value *val) { - v8::Maybe res = obj->obj->Set(ctx_ref->context, key->val, val->val); + v8::Maybe res = obj->obj->Set(ctx_ref->context, key->val, val->val); } void v8_ObjectSetInternalField(v8_local_object *obj, size_t index, v8_local_value *val) { - obj->obj->SetInternalField(index, val->val); + obj->obj->SetInternalField(index, val->val); } v8_local_value* v8_ObjectGetInternalField(v8_local_object *obj, size_t index) { - v8::Local val = obj->obj->GetInternalField(index); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = obj->obj->GetInternalField(index); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } void v8_ObjectFreeze(v8_context_ref *ctx_ref, v8_local_object *obj) { - obj->obj->SetIntegrityLevel(ctx_ref->context, v8::IntegrityLevel::kFrozen); + obj->obj->SetIntegrityLevel(ctx_ref->context, v8::IntegrityLevel::kFrozen); } void v8_FreeObject(v8_local_object *obj) { - V8_FREE(obj); + V8_FREE(obj); } size_t v8_GetInternalFieldCount(v8_local_object *obj) { - return obj->obj->InternalFieldCount(); + return obj->obj->InternalFieldCount(); } void v8_FreeExternalData(v8_local_external_data *ext) { - V8_FREE(ext); + V8_FREE(ext); } v8_local_value* v8_ObjectToValue(v8_local_object *obj) { - v8::Local val = v8::Local::Cast(obj->obj); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(obj->obj); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_value* v8_ValueToValue(v8_local_value *val) { - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val->val); - return res; + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val->val); + return res; } v8_local_value* v8_ExternalDataToValue(v8_local_external_data *ext) { - v8::Local val = v8::Local::Cast(ext->ext); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(ext->ext); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_set* v8_NewSet(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local set = v8::Set::New(isolate); - v8_local_set *res = (v8_local_set*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_set(set); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local set = v8::Set::New(isolate); + v8_local_set *res = (v8_local_set*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_set(set); + return res; } /* Add a value to the set */ void v8_SetAdd(v8_context_ref *ctx_ref, v8_local_set *set, v8_local_value *val) { - v8::MaybeLocal res = set->set->Add(ctx_ref->context, val->val); + v8::MaybeLocal res = set->set->Add(ctx_ref->context, val->val); } v8_local_array* v8_SetAsArray(v8_local_set *set) { - v8::Local arr = set->set->AsArray(); - v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array(arr); - return res; + v8::Local arr = set->set->AsArray(); + v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array(arr); + return res; } /* Convert the given JS set into JS generic value */ v8_local_value* v8_SetToValue(v8_local_set *set) { - v8::Local val = v8::Local::Cast(set->set); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(set->set); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } /* Convert the generic JS value into a JS set */ v8_local_set* v8_ValueAsSet(v8_local_value *val) { - v8::Local set = v8::Local::Cast(val->val); - v8_local_set *res = (v8_local_set*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_set(set); - return res; + v8::Local set = v8::Local::Cast(val->val); + v8_local_set *res = (v8_local_set*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_set(set); + return res; } /* Return 1 if the given JS value is a set and 0 otherwise */ int v8_ValueIsSet(v8_local_value *val) { - return val->val->IsSet(); + return val->val->IsSet(); } void v8_FreeSet(v8_local_set *set) { - V8_FREE(set); + V8_FREE(set); } v8_local_value* v8_NewBool(v8_isolate *i, int val) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local b = v8::Boolean::New(isolate, val); - v8::Local v = v8::Local::Cast(b); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(v); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local b = v8::Boolean::New(isolate, val); + v8::Local v = v8::Local::Cast(b); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(v); + return res; } v8_local_value* v8_NewNull(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local n = v8::Null(isolate); - v8::Local v = v8::Local::Cast(n); + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local n = v8::Null(isolate); + v8::Local v = v8::Local::Cast(n); - v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); - v8_val = new (v8_val) v8_local_value(v); - return v8_val; + v8_local_value *v8_val = (struct v8_local_value*)V8_ALLOC(sizeof(*v8_val)); + v8_val = new (v8_val) v8_local_value(v); + return v8_val; } int v8_ValueIsNull(v8_local_value *val) { - return val->val->IsNull(); + return val->val->IsNull(); } int v8_ValueIsUndefined(v8_local_value *val) { - return val->val->IsUndefined(); + return val->val->IsUndefined(); } v8_local_array_buff* v8_NewArrayBuffer(v8_isolate *i, const char *data, size_t len) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local arr_buff = v8::ArrayBuffer::New(isolate, len); - void *buff = arr_buff->GetBackingStore()->Data(); - memcpy(buff, data, len); - v8_local_array_buff *res = (v8_local_array_buff*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array_buff(arr_buff); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local arr_buff = v8::ArrayBuffer::New(isolate, len); + void *buff = arr_buff->GetBackingStore()->Data(); + memcpy(buff, data, len); + v8_local_array_buff *res = (v8_local_array_buff*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array_buff(arr_buff); + return res; } v8_local_value* v8_ArrayBufferToValue(v8_local_array_buff *arr_buffer) { - v8::Local val = v8::Local::Cast(arr_buffer->arr_buff); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(arr_buffer->arr_buff); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } const void* v8_ArrayBufferGetData(v8_local_array_buff *arr_buffer, size_t *len) { - *len = arr_buffer->arr_buff->ByteLength(); - return arr_buffer->arr_buff->GetBackingStore()->Data(); + *len = arr_buffer->arr_buff->ByteLength(); + return arr_buffer->arr_buff->GetBackingStore()->Data(); } void v8_FreeArrayBuffer(v8_local_array_buff *arr_buffer) { - V8_FREE(arr_buffer); + V8_FREE(arr_buffer); } v8_local_array* v8_NewArray(v8_isolate *i, v8_local_value *const *vals, size_t len) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Local vals_arr[len]; - for (size_t i = 0 ; i < len ; ++i) { - vals_arr[i] = vals[i]->val; - } - v8::Local arr = v8::Array::New(isolate, vals_arr, len); - v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array(arr); - return res; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Local vals_arr[len]; + for (size_t i = 0 ; i < len ; ++i) { + vals_arr[i] = vals[i]->val; + } + v8::Local arr = v8::Array::New(isolate, vals_arr, len); + v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array(arr); + return res; } void v8_FreeArray(v8_local_array *arr) { - V8_FREE(arr); + V8_FREE(arr); } size_t v8_ArrayLen(v8_local_array *arr) { - return arr->arr->Length(); + return arr->arr->Length(); } v8_local_value* v8_ArrayGet(v8_context_ref *ctx_ref, v8_local_array *arr, size_t index) { - v8::MaybeLocal maybe_val = arr->arr->Get(ctx_ref->context, index); - if (maybe_val.IsEmpty()) { - return NULL; - } - v8::Local val = maybe_val.ToLocalChecked(); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::MaybeLocal maybe_val = arr->arr->Get(ctx_ref->context, index); + if (maybe_val.IsEmpty()) { + return NULL; + } + v8::Local val = maybe_val.ToLocalChecked(); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_value* v8_ArrayToValue(v8_local_array *arr) { - v8::Local val = v8::Local::Cast(arr->arr); - v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_value(val); - return res; + v8::Local val = v8::Local::Cast(arr->arr); + v8_local_value *res = (v8_local_value*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_value(val); + return res; } v8_local_array* v8_ValueAsArray(v8_local_value *val) { - v8::Local arr = v8::Local::Cast(val->val); - v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array(arr); - return res; + v8::Local arr = v8::Local::Cast(val->val); + v8_local_array *res = (v8_local_array*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array(arr); + return res; } v8_local_array_buff* v8_ValueAsArrayBuffer(v8_local_value *val) { - v8::Local arr = v8::Local::Cast(val->val); - v8_local_array_buff *res = (v8_local_array_buff*) V8_ALLOC(sizeof(*res)); - res = new (res) v8_local_array_buff(arr); - return res; + v8::Local arr = v8::Local::Cast(val->val); + v8_local_array_buff *res = (v8_local_array_buff*) V8_ALLOC(sizeof(*res)); + res = new (res) v8_local_array_buff(arr); + return res; } v8_persisted_value* v8_PersistValue(v8_isolate *i, v8_local_value *val) { - v8::Isolate *isolate = (v8::Isolate*)i; - return (v8_persisted_value*) new v8::Persistent(isolate, val->val); + v8::Isolate *isolate = (v8::Isolate*)i; + return (v8_persisted_value*) new v8::Persistent(isolate, val->val); } v8_local_value* v8_PersistedValueToLocal(v8_isolate *i, v8_persisted_value *val) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Persistent *persisted_val = (v8::Persistent*)val; - v8_local_value *local_val = (struct v8_local_value*)V8_ALLOC(sizeof(*local_val)); - local_val = new (local_val) v8_local_value(isolate, persisted_val); - return local_val; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Persistent *persisted_val = (v8::Persistent*)val; + v8_local_value *local_val = (struct v8_local_value*)V8_ALLOC(sizeof(*local_val)); + local_val = new (local_val) v8_local_value(isolate, persisted_val); + return local_val; } void v8_FreePersistedValue(v8_persisted_value *val) { - v8::Persistent *persisted_val = (v8::Persistent*)val; - persisted_val->Reset(); - delete persisted_val; + v8::Persistent *persisted_val = (v8::Persistent*)val; + persisted_val->Reset(); + delete persisted_val; } void v8_FreeValue(v8_local_value *val) { - V8_FREE(val); + V8_FREE(val); } v8_utf8_value* v8_ToUtf8(v8_isolate *i, v8_local_value* val) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8_utf8_value *utf8_val = (struct v8_utf8_value*)V8_ALLOC(sizeof(*utf8_val)); - utf8_val = new (utf8_val) v8_utf8_value(isolate, val->val); - return utf8_val; + v8::Isolate *isolate = (v8::Isolate*)i; + v8_utf8_value *utf8_val = (struct v8_utf8_value*)V8_ALLOC(sizeof(*utf8_val)); + utf8_val = new (utf8_val) v8_utf8_value(isolate, val->val); + return utf8_val; } void v8_FreeUtf8(v8_utf8_value *val) { - val->~v8_utf8_value(); - V8_FREE(val); + val->~v8_utf8_value(); + V8_FREE(val); } const char* v8_Utf8PtrLen(v8_utf8_value *val, size_t *len) { - *len = val->utf8_val.length(); - return *val->utf8_val; + *len = val->utf8_val.length(); + return *val->utf8_val; } v8_unlocker* v8_NewUnlocker(v8_isolate *i) { - v8::Isolate *isolate = (v8::Isolate*)i; - v8::Unlocker *unlocker = new v8::Unlocker(isolate); - return (v8_unlocker*)unlocker; + v8::Isolate *isolate = (v8::Isolate*)i; + v8::Unlocker *unlocker = new v8::Unlocker(isolate); + return (v8_unlocker*)unlocker; } void v8_FreeUnlocker(v8_unlocker* u) { - v8::Unlocker *unlocker = (v8::Unlocker*)u; - delete unlocker; + v8::Unlocker *unlocker = (v8::Unlocker*)u; + delete unlocker; } } diff --git a/v8_c_api/src/v8_c_api.h b/v8_c_api/src/v8_c_api.h index 05f43ca..de07f16 100644 --- a/v8_c_api/src/v8_c_api.h +++ b/v8_c_api/src/v8_c_api.h @@ -9,101 +9,101 @@ #include -/* Allocator definition +/** Allocator definition * Note: only structs memory will be allocated using the allocator, * v8 memory will be allocate and manage by b8. */ -typedef struct v8_alloctor { +typedef struct v8_allocator { void* (*v8_Alloc)(size_t bytes); void* (*v8_Realloc)(void *ptr, size_t bytes); void (*v8_Free)(void *ptr); void* (*v8_Calloc)(size_t nmemb, size_t size); char* (*v8_Strdup)(const char *str); -}v8_alloctor; +} v8_allocator; -/* Opaque struct representing a v8 interpreter. +/** Opaque struct representing a v8 interpreter. * There is no limit to the amount of isolates that can be * created in a single processes. */ typedef struct v8_isolate v8_isolate; -/* Represent a scope to run JS code inside the isolate. */ +/** Represent a scope to run JS code inside the isolate. */ typedef struct v8_isolate_scope v8_isolate_scope; -/* An isolate JS environment to run JS code. +/** An isolate JS environment to run JS code. * There is no limit to the amount of contexts that can be * create in a single isolate. Each context has its own globals * separate from other contexts. It is only possible to run a single * contexts in a given time (in each isolate) */ typedef struct v8_context v8_context; -/* Represent a scope to run JS code inside the context. */ +/** Represent a scope to run JS code inside the context. */ typedef struct v8_context_ref v8_context_ref; -/* Try catch scope, any error that will be raise during JS execution +/** Try catch scope, any error that will be raise during JS execution * will be catch by this object. */ typedef struct v8_trycatch v8_trycatch; -/* Responsible for all local handlers. When freed, all the locals +/** Responsible for all local handlers. When freed, all the locals * handlers that were manager by the handlers score will be freed. */ typedef struct v8_handlers_scope v8_handlers_scope; -/* JS String object */ +/** JS String object */ typedef struct v8_local_string v8_local_string; -/* JS native function template */ +/** JS native function template */ typedef struct v8_local_native_function_template v8_local_native_function_template; -/* JS native function */ +/** JS native function */ typedef struct v8_local_native_function v8_local_native_function; -/* JS native object template */ +/** JS native object template */ typedef struct v8_local_object_template v8_local_object_template; -/* JS native object */ +/** JS native object */ typedef struct v8_local_object v8_local_object; typedef struct v8_local_external_data v8_local_external_data; -/* JS native set */ +/** JS native set */ typedef struct v8_local_set v8_local_set; -/* JS native array*/ +/** JS native array*/ typedef struct v8_local_array v8_local_array; -/* JS native array buffer*/ +/** JS native array buffer*/ typedef struct v8_local_array_buff v8_local_array_buff; -/* JS script object */ +/** JS script object */ typedef struct v8_local_script v8_local_script; typedef struct v8_persisted_script v8_persisted_script; typedef struct v8_persisted_object_template v8_persisted_object_template; -/* JS module object */ +/** JS module object */ typedef struct v8_local_module v8_local_module; -/* JS persisted module object */ +/** JS persisted module object */ typedef struct v8_persisted_module v8_persisted_module; -/* JS generic value */ +/** JS generic value */ typedef struct v8_local_value v8_local_value; -/* JS promise object */ +/** JS promise object */ typedef struct v8_local_promise v8_local_promise; -/* JS promise resolver object */ +/** JS promise resolver object */ typedef struct v8_local_resolver v8_local_resolver; -/* Represent a native function arguments */ +/** Represent a native function arguments */ typedef struct v8_local_value_arr v8_local_value_arr; -/* JS utf8 object */ +/** JS utf8 object */ typedef struct v8_utf8_value v8_utf8_value; -/* JS persisted object, can outlive the handlers score. */ +/** JS persisted object, can outlive the handlers score. */ typedef struct v8_persisted_value v8_persisted_value; -/* JS persisted object, can outlive the handlers score. */ +/** JS persisted object, can outlive the handlers score. */ typedef struct v8_unlocker v8_unlocker; typedef void (*v8_InterruptCallback)(v8_isolate *isolate, void* data); @@ -113,19 +113,20 @@ typedef void (*v8_InterruptCallback)(v8_isolate *isolate, void* data); * Returns and int of value 1 on successfull initialisation, 0 * otherwise. */ -int v8_Initialize(v8_alloctor *allocator); +int v8_Initialize(v8_allocator *allocator); /** Initialize default platform, must be called on the process main thread before any v8 API (including `v8_Initialize`). * Returns and int of value 1 on successfull initialisation, 0 otherwise. */ int v8_InitializePlatform(int thread_pool_size, const char *flags); +/** Returns the version of V8 as an ASCII string. */ const char* v8_Version(); -/* Dispose v8 initialization */ +/** Dispose v8 initialization */ void v8_Dispose(); -/* Create a new v8 isolate. An isolate is a v8 interpreter that responsible to run JS code. +/** Creates a new v8 isolate. An isolate is a v8 interpreter that responsible to run JS code. * Impeder may create as many isolates as wishes. * initial_heap_size_in_bytes - the initial isolate heap size * maximum_heap_size_in_bytes - maximum isolate heap size. when this value reached, @@ -133,106 +134,106 @@ void v8_Dispose(); * will abort the processes with OOM error. */ v8_isolate* v8_NewIsolate(size_t initial_heap_size_in_bytes, size_t maximum_heap_size_in_bytes); -/* Set fatal error handler, this method should write the error to some log file, when return the processes will exit */ +/** Set fatal error handler, this method should write the error to some log file, when return the processes will exit */ void v8_IsolateSetFatalErrorHandler(v8_isolate* i, void (*fatal_hanlder)(const char* location, const char* message)); -/* Set OOM error handler, this method should write the error to some log file, when return the processes will exit */ +/** Set OOM error handler, this method should write the error to some log file, when return the processes will exit */ void v8_IsolateSetOOMErrorHandler(v8_isolate* i, void (*oom_hanlder)(const char* location, int is_heap_oom)); -/* Set near OOM handler, the callback will be called when almost reaching OOM and allow to increase the max memory to avoid OOM error. */ +/** Set near OOM handler, the callback will be called when almost reaching OOM and allow to increase the max memory to avoid OOM error. */ void v8_IsolateSetNearOOMHandler(v8_isolate* i, size_t (*near_oom_callback)(void* data, size_t current_heap_limit, size_t initial_heap_limit), void *pd, void(*free_pd)(void*)); -/* Request a GC run for testing */ +/** Request a GC run for testing */ void v8_RequestGCFromTesting(v8_isolate* i, int full); -/* Return the currently used heap size */ +/** Return the currently used heap size */ size_t v8_IsolateUsedHeapSize(v8_isolate* i); -/* Return the currently total heap size */ +/** Return the currently total heap size */ size_t v8_IsolateTotalHeapSize(v8_isolate* i); -/* Return the current heap size limit */ +/** Return the current heap size limit */ size_t v8_IsolateHeapSizeLimit(v8_isolate* i); void v8_IsolateNotifyMemoryPressure(v8_isolate* i); -/* Return the currently used isolate or NULL if not isolate is in used */ +/** Return the currently used isolate or NULL if not isolate is in used */ v8_isolate* v8_IsolateGetCurrent(); -/* Terminate the current JS code running on the given isolate */ +/** Terminate the current JS code running on the given isolate */ void v8_TerminateCurrExecution(v8_isolate* i); -/* Cancel the termination triggered by v8_TerminateCurrExecution, after calling this API it is possible to continue using the isolate */ +/** Cancel the termination triggered by v8_TerminateCurrExecution, after calling this API it is possible to continue using the isolate */ void v8_CancelTerminateExecution(v8_isolate* i); -/* Free the give isolate */ +/** Free the give isolate */ void v8_FreeIsolate(v8_isolate* isolate); void v8_RequestInterrupt(v8_isolate* isolate, v8_InterruptCallback callback, void *data); -/* Enter the given isolate. This function should be called before running any JS +/** Enter the given isolate. This function should be called before running any JS * code on the isolate. */ v8_isolate_scope* v8_IsolateEnter(v8_isolate *v8_isolate); -/* Exit the isolate, after execute this function it is not allowed to run +/** Exit the isolate, after execute this function it is not allowed to run * any more JS on this isolate until `v8_IsolateEnter` is called again. */ void v8_IsolateExit(v8_isolate_scope *v8_isolate_scope); -/* Raise an exception, the given value will be treated as the exception value. */ +/** Raise an exception, the given value will be treated as the exception value. */ void v8_IsolateRaiseException(v8_isolate *isolate, v8_local_value *value); -/* Get current run context from the of this isolate */ +/** Get current run context from the of this isolate */ v8_context_ref* v8_GetCurrentCtxRef(v8_isolate *isolate); void v8_IdleNotificationDeadline(v8_isolate *isolate, double deadline_in_seconds); -/* Create a new try catch object, any exception that will be raise during the JS execution +/** Create a new try catch object, any exception that will be raise during the JS execution * will be catch by this object. */ v8_trycatch* v8_NewTryCatch(v8_isolate *isolate); -/* Return the exception that was catch by the try catch object */ +/** Return the exception that was catch by the try catch object */ v8_local_value* v8_TryCatchGetException(v8_trycatch *trycatch); -/* Returns the trace of the catch exception */ +/** Returns the trace of the catch exception */ v8_local_value* v8_TryCatchGetTrace(v8_trycatch *trycatch, v8_context_ref* ctx); -/* Return true if the execution was terminated using v8_TerminateCurrExecution */ +/** Return true if the execution was terminated using v8_TerminateCurrExecution */ int v8_TryCatchHasTerminated(v8_trycatch *trycatch); -/* Free the try catch object */ +/** Free the try catch object */ void v8_FreeTryCatch(v8_trycatch *trycatch); -/* Create a new handlers scope, the handler scope is responsible to collect all local +/** Create a new handlers scope, the handler scope is responsible to collect all local * handlers that was created while this object is alive. When freed, mark all the local * handlers that was collected for GC. */ v8_handlers_scope* v8_NewHandlersScope(v8_isolate *v8_isolate); -/* Free the given handlers crope */ +/** Free the given handlers crope */ void v8_FreeHandlersScope(v8_handlers_scope* v8_handlersScope); -/* Create a new JS context, a context is an isolate environment to run JS code. +/** Create a new JS context, a context is an isolate environment to run JS code. * A context has his own globals which are not shared with other contexts. * It is only possible to run a single context on a given time (per isolate). */ v8_context* v8_NewContext(v8_isolate* v8_isolate, v8_local_object_template *globals); -/* Free the given context */ +/** Free the given context */ void v8_FreeContext(v8_context* ctx); -/* Set a private data on the given context. +/** Set a private data on the given context. * The private data can later be retrieve using `v8_GetPrivateData`. */ void v8_SetPrivateData(v8_context* ctx, size_t index, void *pd); -/* Resets the data at the specified slot. */ +/** Resets the data at the specified slot. */ void v8_ResetPrivateData(v8_context* ctx, size_t index); -/* Resets the data at the specified slot sing a reference to v8_context. */ +/** Resets the data at the specified slot sing a reference to v8_context. */ void v8_ResetPrivateDataOnCtxRef(v8_context_ref* ctx_ref, size_t index); -/* Return the private data that was set using `v8_SetPrivateData` or NULL +/** Return the private data that was set using `v8_SetPrivateData` or NULL * if no data was set on the given slot. */ void* v8_GetPrivateData(v8_context* ctx, size_t index); -/* Enter the given context, this function must be called befor running any +/** Enter the given context, this function must be called befor running any * JS code on the given context. */ v8_context_ref* v8_ContextEnter(v8_context *v8_ctx); @@ -240,71 +241,71 @@ v8_isolate* v8_ContextRefGetIsolate(v8_context_ref *v8_ctx_ref); v8_local_object* v8_ContextRefGetGlobals(v8_context_ref *v8_ctx_ref); -/* Exit the JS context */ +/** Exit the JS context */ void v8_ExitContextRef(v8_context_ref *v8_ctx_ref); -/* Free the JS context */ +/** Free the JS context */ void v8_FreeContextRef(v8_context_ref *v8_ctx_ref); -/* Same as `v8_GetPrivateData` but works on `v8_context_ref` */ +/** Same as `v8_GetPrivateData` but works on `v8_context_ref` */ void* v8_GetPrivateDataFromCtxRef(v8_context_ref* ctx_ref, size_t index); -/* Same as `v8_SetPrivateData` but works on `v8_context_ref` */ +/** Same as `v8_SetPrivateData` but works on `v8_context_ref` */ void v8_SetPrivateDataOnCtxRef(v8_context_ref* ctx_ref, size_t index, void *pd); -/* Create a new JS string object */ +/** Create a new JS string object */ v8_local_string* v8_NewString(v8_isolate* v8_isolate, const char *str, size_t len); -/* Convert the JS string to JS generic value */ +/** Convert the JS string to JS generic value */ v8_local_value* v8_StringToValue(v8_local_string *str); -/* Convert the JS string to JS string object (same as writing 'new String(...)')*/ +/** Convert the JS string to JS string object (same as writing 'new String(...)')*/ v8_local_object* v8_StringToStringObject(v8_isolate* v8_isolate, v8_local_string *str); -/* Free the given JS string */ +/** Free the given JS string */ void v8_FreeString(v8_local_string *str); -/* Native function callback definition */ +/** Native function callback definition */ typedef v8_local_value* (*native_funcion)(v8_local_value_arr *args, size_t len, void *pd); -/* Create a native function callback template */ +/** Create a native function callback template */ v8_local_native_function_template* v8_NewNativeFunctionTemplate(v8_isolate* i, native_funcion func, void *pd, void(*freePD)(void *pd)); -/* Create a native JS function from the given JS native function template */ +/** Create a native JS function from the given JS native function template */ v8_local_native_function* v8_NativeFunctionTemplateToFunction(v8_context_ref *ctx_ref, v8_local_native_function_template *func); v8_local_native_function* v8_NewNativeFunction(v8_context_ref *ctx_ref, native_funcion func, void *pd, void(*freePD)(void *pd)); -/* Free the given native function template */ +/** Free the given native function template */ void v8_FreeNativeFunctionTemplate(v8_local_native_function_template *func); -/* Free the given native function */ +/** Free the given native function */ void v8_FreeNativeFunction(v8_local_native_function *func); -/* Convert the native function into a generic JS value */ +/** Convert the native function into a generic JS value */ v8_local_value* v8_NativeFunctionToValue(v8_local_native_function *func); -/* Return the i-th index from the native function arguments */ +/** Return the i-th index from the native function arguments */ v8_local_value* v8_ArgsGet(v8_local_value_arr *args, size_t i); v8_local_object* v8_ArgsGetSelf(v8_local_value_arr *args); -/* Return current isolate from the native function arguments */ +/** Return current isolate from the native function arguments */ v8_isolate* v8_GetCurrentIsolate(v8_local_value_arr *args); -/* Create a new JS object template */ +/** Create a new JS object template */ v8_local_object_template* v8_NewObjectTemplate(v8_isolate* v8_isolate); -/* Free the given JS object template */ +/** Free the given JS object template */ void v8_FreeObjectTemplate(v8_local_object_template *obj); -/* Set a function template on the given object template at the given key */ +/** Set a function template on the given object template at the given key */ void v8_ObjectTemplateSetFunction(v8_local_object_template *obj, v8_local_string *name, v8_local_native_function_template *f); -/* Set an object template on the given object template at the given key */ +/** Set an object template on the given object template at the given key */ void v8_ObjectTemplateSetObject(v8_local_object_template *obj, v8_local_string *name, v8_local_object_template *o); -/* Set a generic JS value on the given object template at the given key */ +/** Set a generic JS value on the given object template at the given key */ void v8_ObjectTemplateSetValue(v8_local_object_template *obj, v8_local_string *name, v8_local_value *val); void v8_ObjectTemplateSetInternalFieldCount(v8_local_object_template *obj, size_t count); @@ -315,10 +316,10 @@ void v8_FreePersistedObjectTemplate(v8_persisted_object_template* obj); v8_local_object_template* v8_PersistedObjectTemplateToLocal(v8_isolate *i, v8_persisted_object_template* obj); -/* Convert the given object template to a generic JS value */ +/** Convert the given object template to a generic JS value */ v8_local_object* v8_ObjectTemplateNewInstance(v8_context_ref *ctx_ref, v8_local_object_template *obj); -/* Compile the given code into a script object */ +/** Compile the given code into a script object */ v8_local_script* v8_Compile(v8_context_ref* v8_ctx_ref, v8_local_string* str); v8_persisted_script* v8_ScriptPersist(v8_isolate *i, v8_local_script* script); @@ -329,15 +330,15 @@ void v8_FreePersistedScript(v8_persisted_script* script); typedef v8_local_module* (*V8_LoadModuleCallback)(v8_context_ref* v8_ctx_ref, v8_local_string* name, int identity_hash); -/* Compile the given code as a module */ +/** Compile the given code as a module */ v8_local_module* v8_CompileAsModule(v8_context_ref* v8_ctx_ref, v8_local_string* name, v8_local_string* code, int is_module); -/* Initialize the module, return 1 on success and 0 on failure */ +/** Initialize the module, return 1 on success and 0 on failure */ int v8_InitiateModule(v8_local_module* m, v8_context_ref* v8_ctx_ref, V8_LoadModuleCallback load_module_callback); int v8_ModuleGetIdentityHash(v8_local_module* m); -/* Evaluate the module code */ +/** Evaluate the module code */ v8_local_value* v8_EvaluateModule(v8_local_module* m, v8_context_ref* v8_ctx_ref); v8_persisted_module* v8_ModulePersist(v8_isolate *i, v8_local_module* m); @@ -346,175 +347,173 @@ v8_local_module* v8_ModuleToLocal(v8_isolate *i, v8_persisted_module* m); void v8_FreePersistedModule(v8_persisted_module* m); -/* Evaluate the module code */ +/** Evaluate the module code */ void v8_FreeModule(v8_local_module* m); -/* Free the given script object */ +/** Free the given script object */ void v8_FreeScript(v8_local_script *script); -/* Run the given script object */ +/** Run the given script object */ v8_local_value* v8_Run(v8_context_ref* v8_ctx_ref, v8_local_script* script); -/* Return 1 if the given JS value is a function and 0 otherwise */ +/** Return 1 if the given JS value is a function and 0 otherwise */ int v8_ValueIsFunction(v8_local_value *val); -/* Invoke the given function */ +/** Invoke the given function */ v8_local_value* v8_FunctionCall(v8_context_ref *v8_ctx_ref, v8_local_value *val, size_t argc, v8_local_value* const* argv); -/* Return 1 if the given JS value is an async function and 0 otherwise */ +/** Return 1 if the given JS value is an async function and 0 otherwise */ int v8_ValueIsAsyncFunction(v8_local_value *val); -/* Return 1 if the given JS value is a string and 0 otherwise */ +/** Return 1 if the given JS value is a string and 0 otherwise */ int v8_ValueIsString(v8_local_value *val); -/* Return 1 if the given JS value is a string object */ +/** Return 1 if the given JS value is a string object */ int v8_ValueIsStringObject(v8_local_value *val); -/* Convert the generic JS value into a JS string */ +/** Convert the generic JS value into a JS string */ v8_local_string* v8_ValueAsString(v8_local_value *val); v8_local_value* v8_ValueFromLong(v8_isolate *i, long long val); -/* Return 1 if the given JS value is a big integer and 0 otherwise */ +/** Return 1 if the given JS value is a big integer and 0 otherwise */ int v8_ValueIsBigInt(v8_local_value *val); long long v8_GetBigInt(v8_local_value *val); -/* Return 1 if the given JS value is a number and 0 otherwise */ +/** Return 1 if the given JS value is a number and 0 otherwise */ int v8_ValueIsNumber(v8_local_value *val); double v8_GetNumber(v8_local_value *val); -/* Return 1 if the given JS value is a number and 0 otherwise */ +/** Return 1 if the given JS value is a number and 0 otherwise */ int v8_ValueIsBool(v8_local_value *val); int v8_GetBool(v8_local_value *val); - v8_local_value* v8_ValueFromDouble(v8_isolate *i, double val); -/* Return 1 if the given JS value is a promise and 0 otherwise */ +/** Return 1 if the given JS value is a promise and 0 otherwise */ int v8_ValueIsPromise(v8_local_value *val); -/* Convert the generic JS value into a JS promise */ +/** Convert the generic JS value into a JS promise */ v8_local_promise* v8_ValueAsPromise(v8_local_value *val); -/* Return 1 if the given JS value is an object and 0 otherwise */ +/** Return 1 if the given JS value is an object and 0 otherwise */ int v8_ValueIsObject(v8_local_value *val); int v8_ValueIsExternalData(v8_local_value *val); -/* Return an array contains the enumerable properties names of the given object */ -v8_local_array* v8_ValueGetPropertyNames(v8_context_ref *ctx_ref, v8_local_object *obj); - -/* Return an array contains the all properties names of the given object */ +/** Return an array contains the all properties names of the given object */ v8_local_array* v8_ValueGetOwnPropertyNames(v8_context_ref *ctx_ref, v8_local_object *obj); -/* Deleted the given propery from the object */ +/** Deleted the given propery from the object */ int v8_DeletePropery(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key); +/** Return an array contains the enumerable property names of the given object */ +v8_local_array* v8_ValueGetPropertyNames(v8_context_ref *ctx_ref, v8_local_object *obj); -/* Return 1 if the given JS value is an array and 0 otherwise */ +/** Return 1 if the given JS value is an array and 0 otherwise */ int v8_ValueIsArray(v8_local_value *val); -/* Return 1 if the given JS value is an array buffer and 0 otherwise */ +/** Return 1 if the given JS value is an array buffer and 0 otherwise */ int v8_ValueIsArrayBuffer(v8_local_value *val); -/* Create a new JS object */ +/** Create a new JS object */ v8_local_object* v8_NewObject(v8_isolate *i); v8_local_external_data* v8_NewExternalData(v8_isolate *i, void *data, void(*free)(void*)); void* v8_ExternalDataGet(v8_local_external_data *ext); -/* create a js object form json string */ +/** create a js object form json string */ v8_local_value* v8_NewObjectFromJsonString(v8_context_ref *ctx_ref, v8_local_string *str); -/* create a json string representation of a JS value */ +/** create a json string representation of a JS value */ v8_local_string* v8_JsonStringify(v8_context_ref *ctx_ref, v8_local_value *val); -/* Convert the generic JS value into a JS object */ +/** Convert the generic JS value into a JS object */ v8_local_object* v8_ValueAsObject(v8_local_value *val); v8_local_external_data* v8_ValueAsExternalData(v8_local_value *val); -/* Convert the generic JS value into a JS resolver*/ +/** Convert the generic JS value into a JS resolver*/ v8_local_resolver* v8_ValueAsResolver(v8_local_value *val); -/* Return the value of a given key from the given JS object */ +/** Return the value of a given key from the given JS object */ v8_local_value* v8_ObjectGet(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key); -/* Set a value inside the object at a given key */ +/** Set a value inside the object at a given key */ void v8_ObjectSet(v8_context_ref *ctx_ref, v8_local_object *obj, v8_local_value *key, v8_local_value *val); void v8_ObjectSetInternalField(v8_local_object *obj, size_t index, v8_local_value *val); v8_local_value* v8_ObjectGetInternalField(v8_local_object *obj, size_t index); -/* Freeze the object, same as Object.freeze. */ +/** Freeze the object, same as Object.freeze. */ void v8_ObjectFreeze(v8_context_ref *ctx_ref, v8_local_object *obj); -/* Free the given JS object */ +/** Free the given JS object */ void v8_FreeObject(v8_local_object *obj); size_t v8_GetInternalFieldCount(v8_local_object *obj); void v8_FreeExternalData(v8_local_external_data *ext); -/* Convert the given JS object into JS generic value */ +/** Convert the given JS object into JS generic value */ v8_local_value* v8_ObjectToValue(v8_local_object *obj); -/* Shellow copy of the given JS value. */ +/** Shallow copy of the given JS value. */ v8_local_value* v8_ValueToValue(v8_local_value *obj); v8_local_value* v8_ExternalDataToValue(v8_local_external_data *ext); -/* Create a new set */ +/** Create a new set */ v8_local_set* v8_NewSet(v8_isolate *i); -/* Add a value to the set */ +/** Add a value to the set */ void v8_SetAdd(v8_context_ref *ctx_ref, v8_local_set *set, v8_local_value *val); -/* Turn the set into an array for iterations. */ +/** Turn the set into an array for iterations. */ v8_local_array* v8_SetAsArray(v8_local_set *set); -/* Convert the given JS set into JS generic value */ +/** Convert the given JS set into JS generic value */ v8_local_value* v8_SetToValue(v8_local_set *set); -/* Convert the generic JS value into a JS set */ +/** Convert the generic JS value into a JS set */ v8_local_set* v8_ValueAsSet(v8_local_value *val); -/* Return 1 if the given JS value is a set and 0 otherwise */ +/** Return 1 if the given JS value is a set and 0 otherwise */ int v8_ValueIsSet(v8_local_value *val); -/* Free the given JS set */ +/** Free the given JS set */ void v8_FreeSet(v8_local_set *set); -/* Create a new boolean */ +/** Create a new boolean */ v8_local_value* v8_NewBool(v8_isolate *i, int val); -/* Create a new JS null */ +/** Create a new JS null */ v8_local_value* v8_NewNull(v8_isolate *i); -/* Return 1 if the given JS value is null 0 otherwise */ +/** Return 1 if the given JS value is null 0 otherwise */ int v8_ValueIsNull(v8_local_value *val); -/* Return 1 if the given JS value is undefined 0 otherwise */ +/** Return 1 if the given JS value is undefined 0 otherwise */ int v8_ValueIsUndefined(v8_local_value *val); -/* Create a js ArrayBuffer */ +/** Create a js ArrayBuffer */ v8_local_array_buff* v8_NewArrayBuffer(v8_isolate *i, const char *data, size_t len); v8_local_value* v8_ArrayBufferToValue(v8_local_array_buff *arr_buffer); -/* Return the underline data of an array buffer */ +/** Return the underline data of an array buffer */ const void* v8_ArrayBufferGetData(v8_local_array_buff *arr_buffer, size_t *len); -/* Free a js ArrayBuffer */ +/** Free a js ArrayBuffer */ void v8_FreeArrayBuffer(v8_local_array_buff *arr_buffer); v8_local_array* v8_NewArray(v8_isolate *i, v8_local_value *const *vals, size_t len); -/* Free the given JS array */ +/** Free the given JS array */ void v8_FreeArray(v8_local_array *arr); size_t v8_ArrayLen(v8_local_array *arr); @@ -523,82 +522,82 @@ v8_local_value* v8_ArrayGet(v8_context_ref *ctx_ref, v8_local_array *arr, size_t v8_local_value* v8_ArrayToValue(v8_local_array *obj); -/* Convert the generic JS value into a JS array */ +/** Convert the generic JS value into a JS array */ v8_local_array* v8_ValueAsArray(v8_local_value *val); -/* Convert the generic JS value into a JS array buffer */ +/** Convert the generic JS value into a JS array buffer */ v8_local_array_buff* v8_ValueAsArrayBuffer(v8_local_value *val); -/* Promise state */ +/** Promise state */ typedef enum v8_PromiseState{ v8_PromiseState_Unknown, v8_PromiseState_Fulfilled, v8_PromiseState_Rejected, v8_PromiseState_Pending }v8_PromiseState; -/* Free the given promise object */ +/** Free the given promise object */ void v8_FreePromise(v8_local_promise* promise); -/* Return the state of the given promise object */ +/** Return the state of the given promise object */ v8_PromiseState v8_PromiseGetState(v8_local_promise* promise); -/* Return the result of the given promise object +/** Return the result of the given promise object * Only applicable when the promise state is v8_PromiseState_Fulfilled or v8_PromiseState_Rejected*/ v8_local_value* v8_PromiseGetResult(v8_local_promise* promise); -/* Set the promise fulfilled/rejected callbacks */ +/** Set the promise fulfilled/rejected callbacks */ void v8_PromiseThen(v8_local_promise* promise, v8_context_ref *ctx_ref, v8_local_native_function *resolve, v8_local_native_function *reject); -/* Set a callback that will be called when the V8_Value will be GC */ +/** Set a callback that will be called when the V8_Value will be GC */ void v8_ValueOnFreed(v8_local_value* value, v8_isolate *i, void(*on_freed)(void*), void *pd); -/* Convert the given promise object into a generic JS value. */ +/** Convert the given promise object into a generic JS value. */ v8_local_value* v8_PromiseToValue(v8_local_promise *promise); -/* Create a new resolver object */ +/** Create a new resolver object */ v8_local_resolver* v8_NewResolver(v8_context_ref *ctx_ref); -/* Free the given resolver object */ +/** Free the given resolver object */ void v8_FreeResolver(v8_local_resolver *resolver); -/* Return a promise object attached to this resolver */ +/** Return a promise object attached to this resolver */ v8_local_promise* v8_ResolverGetPromise(v8_local_resolver *resolver); -/* Resolve the resolver object with the given value */ +/** Resolve the resolver object with the given value */ void v8_ResolverResolve(v8_context_ref *ctx_ref, v8_local_resolver *resolver, v8_local_value *val); -/* Reject the resolver object with the given value */ +/** Reject the resolver object with the given value */ void v8_ResolverReject(v8_context_ref *ctx_ref, v8_local_resolver *resolver, v8_local_value *val); -/* Convert the given resolver object into a generic JS value */ +/** Convert the given resolver object into a generic JS value */ v8_local_value* v8_ResolverToValue(v8_local_resolver *resolver); -/* Persist the generic JS value, this function allows to escape the handlers scope and save +/** Persist the generic JS value, this function allows to escape the handlers scope and save * the given object for unlimited time with out worry about GC. */ v8_persisted_value* v8_PersistValue(v8_isolate *i, v8_local_value *val); -/* Turn the persisted value back to local value */ +/** Turn the persisted value back to local value */ v8_local_value* v8_PersistedValueToLocal(v8_isolate *i, v8_persisted_value *val); -/* Free the given persisted value */ +/** Free the given persisted value */ void v8_FreePersistedValue(v8_persisted_value *val); -/* Free the given generic JS value */ +/** Free the given generic JS value */ void v8_FreeValue(v8_local_value *val); -/* Convert the given generic JS value to utf8. +/** Convert the given generic JS value to utf8. * On failure, returns NULL.*/ v8_utf8_value* v8_ToUtf8(v8_isolate *isolate, v8_local_value *val); -/* Free the given uft8 object */ +/** Free the given uft8 object */ void v8_FreeUtf8(v8_utf8_value *val); -/* Return const pointer and length of the utf8 object */ +/** Return const pointer and length of the utf8 object */ const char* v8_Utf8PtrLen(v8_utf8_value *val, size_t *len); -/* Create an unlocker object that will unlock the v8 lock, +/** Create an unlocker object that will unlock the v8 lock, * to re-aquire the lock the unlocker need to be freed */ v8_unlocker* v8_NewUnlocker(v8_isolate *i); -/* Free the unlocker and re-aquire the lock */ +/** Free the unlocker and re-aquire the lock */ void v8_FreeUnlocker(v8_unlocker* unlocker); #endif /* SRC_V8_C_API_H_ */