Skip to content

Commit

Permalink
Hacked ObjectTemplate.SetAccessorProperty body
Browse files Browse the repository at this point in the history
Time to try this out
  • Loading branch information
stroiman committed Nov 6, 2024
1 parent 18b92fb commit 60f9e09
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
12 changes: 12 additions & 0 deletions object_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import "C"
import (
"errors"
"runtime"
"unsafe"
)

// PropertyAttribute are the attribute flags for a property on an Object.
Expand Down Expand Up @@ -66,6 +67,17 @@ func (o *ObjectTemplate) SetInternalFieldCount(fieldCount uint32) {
C.ObjectTemplateSetInternalFieldCount(o.ptr, C.int(fieldCount))
}

func (o *ObjectTemplate) SetAccessorProperty(
key string,
get FunctionTemplate,
set FunctionTemplate,
) {
ckey := C.CString(key)
defer C.free(unsafe.Pointer(ckey))

C.ObjectTemplateSetAccessorProperty(o.ptr, ckey, get.ptr, set.ptr)
}

// InternalFieldCount returns the number of internal fields that instances of this
// template will have.
func (o *ObjectTemplate) InternalFieldCount() uint32 {
Expand Down
21 changes: 21 additions & 0 deletions v8go.cc
Original file line number Diff line number Diff line change
Expand Up @@ -505,6 +505,27 @@ int ObjectTemplateInternalFieldCount(TemplatePtr ptr) {
return obj_tmpl->InternalFieldCount();
}

void ObjectTemplateSetAccessorProperty(TemplatePtr ptr, const char* key, TemplatePtr get, TemplatePtr set)
{
LOCAL_TEMPLATE(ptr);

/*
*
Isolate* iso = tmpl_ptr->iso; \
Locker locker(iso); \
Isolate::Scope isolate_scope(iso); \
HandleScope handle_scope(iso); \
Local<Template> tmpl = tmpl_ptr->ptr.Get(iso);
*/
Local<String> key_val =
String::NewFromUtf8(iso, key, NewStringType::kNormal).ToLocalChecked();
Local<ObjectTemplate> obj_tmpl = tmpl.As<ObjectTemplate>();
Local<FunctionTemplate> get_tmpl = get->ptr.Get(iso).As<FunctionTemplate>();
Local<FunctionTemplate> set_tmpl = set->ptr.Get(iso).As<FunctionTemplate>();

return obj_tmpl->SetAccessorProperty(key_val, get_tmpl, set_tmpl);
}

/********** FunctionTemplate **********/

static void FunctionTemplateCallback(const FunctionCallbackInfo<Value>& info) {
Expand Down
1 change: 1 addition & 0 deletions v8go.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ extern RtnValue ObjectTemplateNewInstance(TemplatePtr ptr, ContextPtr ctx_ptr);
extern void ObjectTemplateSetInternalFieldCount(TemplatePtr ptr,
int field_count);
extern int ObjectTemplateInternalFieldCount(TemplatePtr ptr);
extern void ObjectTemplateSetAccessorProperty(TemplatePtr ptr, const char* key, TemplatePtr get, TemplatePtr set);

extern TemplatePtr NewFunctionTemplate(IsolatePtr iso_ptr, int callback_ref);
extern RtnValue FunctionTemplateGetFunction(TemplatePtr ptr,
Expand Down

0 comments on commit 60f9e09

Please sign in to comment.