Skip to content

Commit

Permalink
Extend constructor to accept resource constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
konradreiche committed May 2, 2019
1 parent 4b66934 commit b41b206
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
16 changes: 14 additions & 2 deletions v8.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ type Isolate struct {
// NewIsolate creates a new V8 Isolate.
func NewIsolate() *Isolate {
v8_init_once.Do(func() { C.v8_init() })
iso := &Isolate{ptr: C.v8_Isolate_New(C.StartupData{ptr: nil, len: 0})}
iso := &Isolate{ptr: C.v8_Isolate_New(C.StartupData{ptr: nil, len: 0}, nil)}
runtime.SetFinalizer(iso, (*Isolate).release)
return iso
}
Expand All @@ -163,7 +163,19 @@ func NewIsolate() *Isolate {
// to initialize all Contexts created from this Isolate.
func NewIsolateWithSnapshot(s *Snapshot) *Isolate {
v8_init_once.Do(func() { C.v8_init() })
iso := &Isolate{ptr: C.v8_Isolate_New(s.data), s: s}
iso := &Isolate{ptr: C.v8_Isolate_New(s.data, nil), s: s}
runtime.SetFinalizer(iso, (*Isolate).release)
return iso
}

type ResourceConstraints struct {
MaxOldSpaceSize int
}

func NewIsolateWithConstraints(constraints ResourceConstraints) *Isolate {
v8_init_once.Do(func() { C.v8_init() })
var c = C.ResourceConstraints{max_old_space_size: C.int(constraints.MaxOldSpaceSize)}
iso := &Isolate{ptr: C.v8_Isolate_New(C.StartupData{ptr: nil, len: 0}, &c)}
runtime.SetFinalizer(iso, (*Isolate).release)
return iso
}
Expand Down
6 changes: 5 additions & 1 deletion v8_c_bridge.cc
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ StartupData v8_CreateSnapshotDataBlob(const char* js) {
return StartupData{data.data, data.raw_size};
}

IsolatePtr v8_Isolate_New(StartupData startup_data) {
IsolatePtr v8_Isolate_New(StartupData startup_data, ResourceConstraints* resource_constraints) {
v8::Isolate::CreateParams create_params;
create_params.array_buffer_allocator = allocator;
if (startup_data.len > 0 && startup_data.ptr != nullptr) {
Expand All @@ -191,6 +191,10 @@ IsolatePtr v8_Isolate_New(StartupData startup_data) {
data->raw_size = startup_data.len;
create_params.snapshot_blob = data;
}
if (resource_constraints != nullptr) {
create_params.constraints = v8::ResourceConstraints();
create_params.constraints.set_max_old_space_size(resource_constraints->max_old_space_size);
}
return static_cast<IsolatePtr>(v8::Isolate::New(create_params));
}
ContextPtr v8_Isolate_NewContext(IsolatePtr isolate_ptr) {
Expand Down
6 changes: 5 additions & 1 deletion v8_c_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ typedef struct {
size_t does_zap_garbage;
} HeapStatistics;

typedef struct {
int max_old_space_size;
} ResourceConstraints;

// NOTE! These values must exactly match the values in kinds.go. Any mismatch
// will cause kinds to be misreported.
typedef enum {
Expand Down Expand Up @@ -114,7 +118,7 @@ extern void v8_init();

extern StartupData v8_CreateSnapshotDataBlob(const char* js);

extern IsolatePtr v8_Isolate_New(StartupData data);
extern IsolatePtr v8_Isolate_New(StartupData data, ResourceConstraints* resource_constraints);
extern ContextPtr v8_Isolate_NewContext(IsolatePtr isolate);
extern void v8_Isolate_Terminate(IsolatePtr isolate);
extern void v8_Isolate_Release(IsolatePtr isolate);
Expand Down

0 comments on commit b41b206

Please sign in to comment.