diff --git a/function_template.cc b/function_template.cc new file mode 100644 index 00000000..9e2e35c7 --- /dev/null +++ b/function_template.cc @@ -0,0 +1,93 @@ +#include "_cgo_export.h" + +#include "deps/include/v8-context.h" +#include "deps/include/v8-function.h" +#include "isolate-macros.h" +#include "template-macros.h" +#include "template.h" + +using namespace v8; + +void FunctionTemplateCallback(const FunctionCallbackInfo& info) { + Isolate* iso = info.GetIsolate(); + ISOLATE_SCOPE(iso); + + // This callback function can be called from any Context, which we only know + // at runtime. We extract the Context reference from the embedder data so that + // we can use the context registry to match the Context on the Go side + Local local_ctx = iso->GetCurrentContext(); + int ctx_ref = local_ctx->GetEmbedderData(1).As()->Value(); + m_ctx* ctx = goContext(ctx_ref); + + int callback_ref = info.Data().As()->Value(); + + m_value* _this = new m_value; + _this->id = 0; + _this->iso = iso; + _this->ctx = ctx; + _this->ptr.Reset(iso, Global(iso, info.This())); + + int args_count = info.Length(); + ValuePtr thisAndArgs[args_count + 1]; + thisAndArgs[0] = tracked_value(ctx, _this); + ValuePtr* args = thisAndArgs + 1; + for (int i = 0; i < args_count; i++) { + m_value* val = new m_value; + val->id = 0; + val->iso = iso; + val->ctx = ctx; + val->ptr.Reset(iso, Global(iso, info[i])); + args[i] = tracked_value(ctx, val); + } + + goFunctionCallback_return retval = + goFunctionCallback(ctx_ref, callback_ref, thisAndArgs, args_count); + if (retval.r1 != nullptr) { + iso->ThrowException(retval.r1->ptr.Get(iso)); + } else if (retval.r0 != nullptr) { + info.GetReturnValue().Set(retval.r0->ptr.Get(iso)); + } else { + info.GetReturnValue().SetUndefined(); + } +} + +TemplatePtr NewFunctionTemplate(IsolatePtr iso, int callback_ref) { + Locker locker(iso); + Isolate::Scope isolate_scope(iso); + HandleScope handle_scope(iso); + + // (rogchap) We only need to store one value, callback_ref, into the + // C++ callback function data, but if we needed to store more items we could + // use an V8::Array; this would require the internal context from + // iso->GetData(0) + Local cbData = Integer::New(iso, callback_ref); + + m_template* ot = new m_template; + ot->iso = iso; + ot->ptr.Reset(iso, + FunctionTemplate::New(iso, FunctionTemplateCallback, cbData)); + return ot; +} + +RtnValue FunctionTemplateGetFunction(m_template* ptr, m_ctx* ctx) { + LOCAL_TEMPLATE(ptr); + TryCatch try_catch(iso); + Local local_ctx = ctx->ptr.Get(iso); + Context::Scope context_scope(local_ctx); + + Local fn_tmpl = tmpl.As(); + RtnValue rtn = {}; + Local fn; + if (!fn_tmpl->GetFunction(local_ctx).ToLocal(&fn)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + + m_value* val = new m_value; + val->id = 0; + val->iso = iso; + val->ctx = ctx; + val->ptr = Global(iso, fn); + rtn.value = tracked_value(ctx, val); + return rtn; +} diff --git a/function_template.go b/function_template.go index d4699226..116a6dd8 100644 --- a/function_template.go +++ b/function_template.go @@ -5,7 +5,7 @@ package v8go // #include -// #include "v8go.h" +// #include "function_template.h" import "C" import ( "runtime" diff --git a/function_template.h b/function_template.h new file mode 100644 index 00000000..ddbae0ff --- /dev/null +++ b/function_template.h @@ -0,0 +1,30 @@ +#ifndef V8GO_FUNCTION_TEMPLATE_H +#define V8GO_FUNCTION_TEMPLATE_H + +#include "errors.h" + +#ifdef __cplusplus + +namespace v8 { +class Isolate; +} + +typedef v8::Isolate v8Isolate; + +extern "C" { +#else + +typedef struct v8Isolate v8Isolate; + +#endif + +typedef struct m_template m_template; +typedef struct m_ctx m_ctx; + +extern m_template* NewFunctionTemplate(v8Isolate* iso_ptr, int callback_ref); +extern RtnValue FunctionTemplateGetFunction(m_template* ptr, m_ctx* ctx_ptr); + +#ifdef __cplusplus +} // extern "C" +#endif +#endif diff --git a/object.cc b/object.cc new file mode 100644 index 00000000..6a63d16c --- /dev/null +++ b/object.cc @@ -0,0 +1,175 @@ +#include "object.h" +#include "deps/include/v8-object.h" +#include "isolate-macros.h" +#include "utils.h" +#include "value-macros.h" +#include "value.h" + +using namespace v8; + +/********** Object **********/ + +#define LOCAL_OBJECT(ptr) \ + LOCAL_VALUE(ptr) \ + Local obj = value.As() + +void ObjectSet(ValuePtr ptr, const char* key, ValuePtr prop_val) { + LOCAL_OBJECT(ptr); + Local key_val = + String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); + obj->Set(local_ctx, key_val, prop_val->ptr.Get(iso)).Check(); +} + +void ObjectSetAnyKey(ValuePtr ptr, ValuePtr key, ValuePtr prop_val) { + LOCAL_OBJECT(ptr); + Local local_key = key->ptr.Get(iso); + obj->Set(local_ctx, local_key, prop_val->ptr.Get(iso)).Check(); +} + +void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr prop_val) { + LOCAL_OBJECT(ptr); + obj->Set(local_ctx, idx, prop_val->ptr.Get(iso)).Check(); +} + +int ObjectSetInternalField(ValuePtr ptr, int idx, ValuePtr val_ptr) { + LOCAL_OBJECT(ptr); + m_value* prop_val = static_cast(val_ptr); + + if (idx >= obj->InternalFieldCount()) { + return 0; + } + + obj->SetInternalField(idx, prop_val->ptr.Get(iso)); + + return 1; +} + +int ObjectInternalFieldCount(ValuePtr ptr) { + LOCAL_OBJECT(ptr); + return obj->InternalFieldCount(); +} + +RtnValue ObjectGet(ValuePtr ptr, const char* key) { + LOCAL_OBJECT(ptr); + RtnValue rtn = {}; + + Local key_val; + if (!String::NewFromUtf8(iso, key, NewStringType::kNormal) + .ToLocal(&key_val)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + Local result; + if (!obj->Get(local_ctx, key_val).ToLocal(&result)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + m_value* new_val = new m_value; + new_val->id = 0; + new_val->iso = iso; + new_val->ctx = ctx; + new_val->ptr = Global(iso, result); + + rtn.value = tracked_value(ctx, new_val); + return rtn; +} + +RtnValue ObjectGetAnyKey(ValuePtr ptr, ValuePtr key) { + LOCAL_OBJECT(ptr); + RtnValue rtn = {}; + + Local local_key = key->ptr.Get(iso); + Local result; + if (!obj->Get(local_ctx, local_key).ToLocal(&result)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + m_value* new_val = new m_value; + new_val->id = 0; + new_val->iso = iso; + new_val->ctx = ctx; + new_val->ptr = Global(iso, result); + + rtn.value = tracked_value(ctx, new_val); + return rtn; +} + +RtnValue ObjectGetInternalField(ValuePtr ptr, int idx) { + LOCAL_OBJECT(ptr); + RtnValue rtn = {}; + + if (idx >= obj->InternalFieldCount()) { + rtn.error.msg = CopyString("internal field index out of range"); + return rtn; + } + + Local result = obj->GetInternalField(idx); + if (!result->IsValue()) { + rtn.error.msg = CopyString("the internal field did not contain a Value"); + return rtn; + } + + m_value* new_val = new m_value; + new_val->id = 0; + new_val->iso = iso; + new_val->ctx = ctx; + new_val->ptr = Global(iso, result.As()); + + rtn.value = tracked_value(ctx, new_val); + return rtn; +} + +RtnValue ObjectGetIdx(ValuePtr ptr, uint32_t idx) { + LOCAL_OBJECT(ptr); + RtnValue rtn = {}; + + Local result; + if (!obj->Get(local_ctx, idx).ToLocal(&result)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + m_value* new_val = new m_value; + new_val->id = 0; + new_val->iso = iso; + new_val->ctx = ctx; + new_val->ptr = Global(iso, result); + + rtn.value = tracked_value(ctx, new_val); + return rtn; +} + +int ObjectHas(ValuePtr ptr, const char* key) { + LOCAL_OBJECT(ptr); + Local key_val = + String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); + return obj->Has(local_ctx, key_val).ToChecked(); +} + +int ObjectHasAnyKey(ValuePtr ptr, ValuePtr key) { + LOCAL_OBJECT(ptr); + Local local_key = key->ptr.Get(iso); + return obj->Has(local_ctx, local_key).ToChecked(); +} + +int ObjectHasIdx(ValuePtr ptr, uint32_t idx) { + LOCAL_OBJECT(ptr); + return obj->Has(local_ctx, idx).ToChecked(); +} + +int ObjectDelete(ValuePtr ptr, const char* key) { + LOCAL_OBJECT(ptr); + Local key_val = + String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); + return obj->Delete(local_ctx, key_val).ToChecked(); +} + +int ObjectDeleteAnyKey(ValuePtr ptr, ValuePtr key) { + LOCAL_OBJECT(ptr); + Local local_key = key->ptr.Get(iso); + return obj->Delete(local_ctx, local_key).ToChecked(); +} + +int ObjectDeleteIdx(ValuePtr ptr, uint32_t idx) { + LOCAL_OBJECT(ptr); + return obj->Delete(local_ctx, idx).ToChecked(); +} diff --git a/object.go b/object.go index 497a14b7..93b05177 100644 --- a/object.go +++ b/object.go @@ -5,7 +5,7 @@ package v8go // #include -// #include "v8go.h" +// #include "object.h" import "C" import ( "fmt" diff --git a/object.h b/object.h new file mode 100644 index 00000000..48b369bc --- /dev/null +++ b/object.h @@ -0,0 +1,33 @@ +#ifndef V8GO_OBJECT_H +#define V8GO_OBJECT_H + +#include + +#include "errors.h" + +#ifdef __cplusplus + +extern "C" { +#endif + +extern void ObjectSet(ValuePtr ptr, const char* key, ValuePtr val_ptr); +extern void ObjectSetAnyKey(ValuePtr ptr, ValuePtr key, ValuePtr val_ptr); +extern void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr val_ptr); +extern int ObjectSetInternalField(ValuePtr ptr, int idx, ValuePtr val_ptr); +extern int ObjectInternalFieldCount(ValuePtr ptr); + +extern RtnValue ObjectGet(ValuePtr ptr, const char* key); +extern RtnValue ObjectGetAnyKey(ValuePtr ptr, ValuePtr key); +extern RtnValue ObjectGetIdx(ValuePtr ptr, uint32_t idx); +extern RtnValue ObjectGetInternalField(ValuePtr ptr, int idx); +int ObjectHas(ValuePtr ptr, const char* key); +int ObjectHasAnyKey(ValuePtr ptr, ValuePtr key); +int ObjectHasIdx(ValuePtr ptr, uint32_t idx); +int ObjectDelete(ValuePtr ptr, const char* key); +int ObjectDeleteAnyKey(ValuePtr ptr, ValuePtr key); +int ObjectDeleteIdx(ValuePtr ptr, uint32_t idx); + +#ifdef __cplusplus +} // extern "C" +#endif +#endif diff --git a/object_template.cc b/object_template.cc new file mode 100644 index 00000000..a23a5655 --- /dev/null +++ b/object_template.cc @@ -0,0 +1,58 @@ +#include "object_template.h" +#include "context.h" +#include "deps/include/v8-context.h" +#include "deps/include/v8-isolate.h" +#include "deps/include/v8-locker.h" +#include "deps/include/v8-template.h" +#include "template-macros.h" + +using namespace v8; + +TemplatePtr NewObjectTemplate(v8Isolate* iso) { + Locker locker(iso); + Isolate::Scope isolate_scope(iso); + HandleScope handle_scope(iso); + + m_template* ot = new m_template; + ot->iso = iso; + ot->ptr.Reset(iso, ObjectTemplate::New(iso)); + return ot; +} + +RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, m_ctx* ctx) { + LOCAL_TEMPLATE(ptr); + TryCatch try_catch(iso); + Local local_ctx = ctx->ptr.Get(iso); + Context::Scope context_scope(local_ctx); + + RtnValue rtn = {}; + + Local obj_tmpl = tmpl.As(); + Local obj; + if (!obj_tmpl->NewInstance(local_ctx).ToLocal(&obj)) { + rtn.error = ExceptionError(try_catch, iso, local_ctx); + return rtn; + } + + m_value* val = new m_value; + val->id = 0; + val->iso = iso; + val->ctx = ctx; + val->ptr = Global(iso, obj); + rtn.value = tracked_value(ctx, val); + return rtn; +} + +void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr, int field_count) { + LOCAL_TEMPLATE(ptr); + + Local obj_tmpl = tmpl.As(); + obj_tmpl->SetInternalFieldCount(field_count); +} + +int ObjectTemplateInternalFieldCount(TemplatePtr ptr) { + LOCAL_TEMPLATE(ptr); + + Local obj_tmpl = tmpl.As(); + return obj_tmpl->InternalFieldCount(); +} diff --git a/object_template.go b/object_template.go index 427eb096..de32cad7 100644 --- a/object_template.go +++ b/object_template.go @@ -5,7 +5,7 @@ package v8go // #include -// #include "v8go.h" +// #include "object_template.h" import "C" import ( "errors" diff --git a/object_template.h b/object_template.h new file mode 100644 index 00000000..68f010be --- /dev/null +++ b/object_template.h @@ -0,0 +1,34 @@ +#ifndef V8GO_OBJECT_TEMPLATE_H +#define V8GO_OBJECT_TEMPLATE_H + +#include "errors.h" +#include "template.h" + +#ifdef __cplusplus + +namespace v8 { +class Isolate; +} + +typedef v8::Isolate v8Isolate; + +extern "C" { +#else + +typedef struct v8Isolate v8Isolate; + +#endif + +typedef struct m_ctx m_ctx; +typedef struct m_template m_template; + +extern TemplatePtr NewObjectTemplate(v8Isolate* iso_ptr); +extern RtnValue ObjectTemplateNewInstance(m_template* ptr, m_ctx* ctx_ptr); +extern void ObjectTemplateSetInternalFieldCount(m_template* ptr, + int field_count); +extern int ObjectTemplateInternalFieldCount(m_template* ptr); + +#ifdef __cplusplus +} +#endif +#endif diff --git a/utils.h b/utils.h index 2a79ad1d..b4502f0f 100644 --- a/utils.h +++ b/utils.h @@ -1,6 +1,11 @@ +#ifndef V8GO_UTILS_H +#define V8GO_UTILS_H + #include #include "deps/include/v8-primitive.h" extern const char* CopyString(std::string str); extern const char* CopyString(v8::String::Utf8Value& value); + +#endif diff --git a/v8go.cc b/v8go.cc index 7f16b9e7..4356fad8 100644 --- a/v8go.cc +++ b/v8go.cc @@ -10,8 +10,6 @@ #include #include "utils.h" -#include "_cgo_export.h" - #include "context-macros.h" #include "isolate-macros.h" #include "template-macros.h" @@ -20,6 +18,8 @@ using namespace v8; +void FunctionTemplateCallback(const FunctionCallbackInfo& info); + const int ScriptCompilerNoCompileOptions = ScriptCompiler::kNoCompileOptions; const int ScriptCompilerConsumeCodeCache = ScriptCompiler::kConsumeCodeCache; const int ScriptCompilerEagerCompile = ScriptCompiler::kEagerCompile; @@ -272,143 +272,6 @@ int TemplateSetAnyTemplate(TemplatePtr ptr, return true; } -/********** ObjectTemplate **********/ - -TemplatePtr NewObjectTemplate(IsolatePtr iso) { - Locker locker(iso); - Isolate::Scope isolate_scope(iso); - HandleScope handle_scope(iso); - - m_template* ot = new m_template; - ot->iso = iso; - ot->ptr.Reset(iso, ObjectTemplate::New(iso)); - return ot; -} - -RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx) { - LOCAL_TEMPLATE(ptr); - TryCatch try_catch(iso); - Local local_ctx = ctx->ptr.Get(iso); - Context::Scope context_scope(local_ctx); - - RtnValue rtn = {}; - - Local obj_tmpl = tmpl.As(); - Local obj; - if (!obj_tmpl->NewInstance(local_ctx).ToLocal(&obj)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - - m_value* val = new m_value; - val->id = 0; - val->iso = iso; - val->ctx = ctx; - val->ptr = Global(iso, obj); - rtn.value = tracked_value(ctx, val); - return rtn; -} - -void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr, int field_count) { - LOCAL_TEMPLATE(ptr); - - Local obj_tmpl = tmpl.As(); - obj_tmpl->SetInternalFieldCount(field_count); -} - -int ObjectTemplateInternalFieldCount(TemplatePtr ptr) { - LOCAL_TEMPLATE(ptr); - - Local obj_tmpl = tmpl.As(); - return obj_tmpl->InternalFieldCount(); -} - -/********** FunctionTemplate **********/ - -static void FunctionTemplateCallback(const FunctionCallbackInfo& info) { - Isolate* iso = info.GetIsolate(); - ISOLATE_SCOPE(iso); - - // This callback function can be called from any Context, which we only know - // at runtime. We extract the Context reference from the embedder data so that - // we can use the context registry to match the Context on the Go side - Local local_ctx = iso->GetCurrentContext(); - int ctx_ref = local_ctx->GetEmbedderData(1).As()->Value(); - m_ctx* ctx = goContext(ctx_ref); - - int callback_ref = info.Data().As()->Value(); - - m_value* _this = new m_value; - _this->id = 0; - _this->iso = iso; - _this->ctx = ctx; - _this->ptr.Reset(iso, Global(iso, info.This())); - - int args_count = info.Length(); - ValuePtr thisAndArgs[args_count + 1]; - thisAndArgs[0] = tracked_value(ctx, _this); - ValuePtr* args = thisAndArgs + 1; - for (int i = 0; i < args_count; i++) { - m_value* val = new m_value; - val->id = 0; - val->iso = iso; - val->ctx = ctx; - val->ptr.Reset(iso, Global(iso, info[i])); - args[i] = tracked_value(ctx, val); - } - - goFunctionCallback_return retval = - goFunctionCallback(ctx_ref, callback_ref, thisAndArgs, args_count); - if (retval.r1 != nullptr) { - iso->ThrowException(retval.r1->ptr.Get(iso)); - } else if (retval.r0 != nullptr) { - info.GetReturnValue().Set(retval.r0->ptr.Get(iso)); - } else { - info.GetReturnValue().SetUndefined(); - } -} - -TemplatePtr NewFunctionTemplate(IsolatePtr iso, int callback_ref) { - Locker locker(iso); - Isolate::Scope isolate_scope(iso); - HandleScope handle_scope(iso); - - // (rogchap) We only need to store one value, callback_ref, into the - // C++ callback function data, but if we needed to store more items we could - // use an V8::Array; this would require the internal context from - // iso->GetData(0) - Local cbData = Integer::New(iso, callback_ref); - - m_template* ot = new m_template; - ot->iso = iso; - ot->ptr.Reset(iso, - FunctionTemplate::New(iso, FunctionTemplateCallback, cbData)); - return ot; -} - -RtnValue FunctionTemplateGetFunction(TemplatePtr ptr, ContextPtr ctx) { - LOCAL_TEMPLATE(ptr); - TryCatch try_catch(iso); - Local local_ctx = ctx->ptr.Get(iso); - Context::Scope context_scope(local_ctx); - - Local fn_tmpl = tmpl.As(); - RtnValue rtn = {}; - Local fn; - if (!fn_tmpl->GetFunction(local_ctx).ToLocal(&fn)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - - m_value* val = new m_value; - val->id = 0; - val->iso = iso; - val->ctx = ctx; - val->ptr = Global(iso, fn); - rtn.value = tracked_value(ctx, val); - return rtn; -} - /********** Context **********/ RtnValue JSONParse(ContextPtr ctx, const char* str) { @@ -1036,173 +899,6 @@ const char* ExceptionGetMessageString(ValuePtr ptr) { return CopyString(utf8); } -/********** Object **********/ - -#define LOCAL_OBJECT(ptr) \ - LOCAL_VALUE(ptr) \ - Local obj = value.As() - -void ObjectSet(ValuePtr ptr, const char* key, ValuePtr prop_val) { - LOCAL_OBJECT(ptr); - Local key_val = - String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); - obj->Set(local_ctx, key_val, prop_val->ptr.Get(iso)).Check(); -} - -void ObjectSetAnyKey(ValuePtr ptr, ValuePtr key, ValuePtr prop_val) { - LOCAL_OBJECT(ptr); - Local local_key = key->ptr.Get(iso); - obj->Set(local_ctx, local_key, prop_val->ptr.Get(iso)).Check(); -} - -void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr prop_val) { - LOCAL_OBJECT(ptr); - obj->Set(local_ctx, idx, prop_val->ptr.Get(iso)).Check(); -} - -int ObjectSetInternalField(ValuePtr ptr, int idx, ValuePtr val_ptr) { - LOCAL_OBJECT(ptr); - m_value* prop_val = static_cast(val_ptr); - - if (idx >= obj->InternalFieldCount()) { - return 0; - } - - obj->SetInternalField(idx, prop_val->ptr.Get(iso)); - - return 1; -} - -int ObjectInternalFieldCount(ValuePtr ptr) { - LOCAL_OBJECT(ptr); - return obj->InternalFieldCount(); -} - -RtnValue ObjectGet(ValuePtr ptr, const char* key) { - LOCAL_OBJECT(ptr); - RtnValue rtn = {}; - - Local key_val; - if (!String::NewFromUtf8(iso, key, NewStringType::kNormal) - .ToLocal(&key_val)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - Local result; - if (!obj->Get(local_ctx, key_val).ToLocal(&result)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - m_value* new_val = new m_value; - new_val->id = 0; - new_val->iso = iso; - new_val->ctx = ctx; - new_val->ptr = Global(iso, result); - - rtn.value = tracked_value(ctx, new_val); - return rtn; -} - -RtnValue ObjectGetAnyKey(ValuePtr ptr, ValuePtr key) { - LOCAL_OBJECT(ptr); - RtnValue rtn = {}; - - Local local_key = key->ptr.Get(iso); - Local result; - if (!obj->Get(local_ctx, local_key).ToLocal(&result)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - m_value* new_val = new m_value; - new_val->id = 0; - new_val->iso = iso; - new_val->ctx = ctx; - new_val->ptr = Global(iso, result); - - rtn.value = tracked_value(ctx, new_val); - return rtn; -} - -RtnValue ObjectGetInternalField(ValuePtr ptr, int idx) { - LOCAL_OBJECT(ptr); - RtnValue rtn = {}; - - if (idx >= obj->InternalFieldCount()) { - rtn.error.msg = CopyString("internal field index out of range"); - return rtn; - } - - Local result = obj->GetInternalField(idx); - if (!result->IsValue()) { - rtn.error.msg = CopyString("the internal field did not contain a Value"); - return rtn; - } - - m_value* new_val = new m_value; - new_val->id = 0; - new_val->iso = iso; - new_val->ctx = ctx; - new_val->ptr = Global(iso, result.As()); - - rtn.value = tracked_value(ctx, new_val); - return rtn; -} - -RtnValue ObjectGetIdx(ValuePtr ptr, uint32_t idx) { - LOCAL_OBJECT(ptr); - RtnValue rtn = {}; - - Local result; - if (!obj->Get(local_ctx, idx).ToLocal(&result)) { - rtn.error = ExceptionError(try_catch, iso, local_ctx); - return rtn; - } - m_value* new_val = new m_value; - new_val->id = 0; - new_val->iso = iso; - new_val->ctx = ctx; - new_val->ptr = Global(iso, result); - - rtn.value = tracked_value(ctx, new_val); - return rtn; -} - -int ObjectHas(ValuePtr ptr, const char* key) { - LOCAL_OBJECT(ptr); - Local key_val = - String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); - return obj->Has(local_ctx, key_val).ToChecked(); -} - -int ObjectHasAnyKey(ValuePtr ptr, ValuePtr key) { - LOCAL_OBJECT(ptr); - Local local_key = key->ptr.Get(iso); - return obj->Has(local_ctx, local_key).ToChecked(); -} - -int ObjectHasIdx(ValuePtr ptr, uint32_t idx) { - LOCAL_OBJECT(ptr); - return obj->Has(local_ctx, idx).ToChecked(); -} - -int ObjectDelete(ValuePtr ptr, const char* key) { - LOCAL_OBJECT(ptr); - Local key_val = - String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked(); - return obj->Delete(local_ctx, key_val).ToChecked(); -} - -int ObjectDeleteAnyKey(ValuePtr ptr, ValuePtr key) { - LOCAL_OBJECT(ptr); - Local local_key = key->ptr.Get(iso); - return obj->Delete(local_ctx, local_key).ToChecked(); -} - -int ObjectDeleteIdx(ValuePtr ptr, uint32_t idx) { - LOCAL_OBJECT(ptr); - return obj->Delete(local_ctx, idx).ToChecked(); -} - /********** Promise **********/ RtnValue NewPromiseResolver(ContextPtr ctx) { diff --git a/v8go.h b/v8go.h index 8eb237db..8945f368 100644 --- a/v8go.h +++ b/v8go.h @@ -104,34 +104,8 @@ extern int TemplateSetAnyTemplate(TemplatePtr ptr, TemplatePtr obj_ptr, int attributes); -extern TemplatePtr NewObjectTemplate(IsolatePtr iso_ptr); -extern RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx_ptr); -extern void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr, - int field_count); -extern int ObjectTemplateInternalFieldCount(TemplatePtr ptr); - -extern TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref); -extern RtnValue FunctionTemplateGetFunction(TemplatePtr ptr, - ContextPtr ctx_ptr); - const char* ExceptionGetMessageString(ValuePtr ptr); -extern void ObjectSet(ValuePtr ptr, const char* key, ValuePtr val_ptr); -extern void ObjectSetAnyKey(ValuePtr ptr, ValuePtr key, ValuePtr val_ptr); -extern void ObjectSetIdx(ValuePtr ptr, uint32_t idx, ValuePtr val_ptr); -extern int ObjectSetInternalField(ValuePtr ptr, int idx, ValuePtr val_ptr); -extern int ObjectInternalFieldCount(ValuePtr ptr); -extern RtnValue ObjectGet(ValuePtr ptr, const char* key); -extern RtnValue ObjectGetAnyKey(ValuePtr ptr, ValuePtr key); -extern RtnValue ObjectGetIdx(ValuePtr ptr, uint32_t idx); -extern RtnValue ObjectGetInternalField(ValuePtr ptr, int idx); -int ObjectHas(ValuePtr ptr, const char* key); -int ObjectHasAnyKey(ValuePtr ptr, ValuePtr key); -int ObjectHasIdx(ValuePtr ptr, uint32_t idx); -int ObjectDelete(ValuePtr ptr, const char* key); -int ObjectDeleteAnyKey(ValuePtr ptr, ValuePtr key); -int ObjectDeleteIdx(ValuePtr ptr, uint32_t idx); - extern RtnValue NewPromiseResolver(ContextPtr ctx_ptr); extern ValuePtr PromiseResolverGetPromise(ValuePtr ptr); int PromiseResolverResolve(ValuePtr ptr, ValuePtr val_ptr);