forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_template.go
77 lines (65 loc) · 2.16 KB
/
object_template.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
// Copyright 2020 Roger Chapman and the v8go contributors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package v8go
// #include <stdlib.h>
// #include "v8go.h"
import "C"
import (
"errors"
"runtime"
)
// PropertyAttribute are the attribute flags for a property on an Object.
// Typical usage when setting an Object or TemplateObject property, and
// can also be validated when accessing a property.
type PropertyAttribute uint8
const (
// None.
None PropertyAttribute = 0
// ReadOnly, ie. not writable.
ReadOnly PropertyAttribute = 1 << iota
// DontEnum, ie. not enumerable.
DontEnum
// DontDelete, ie. not configurable.
DontDelete
)
// ObjectTemplate is used to create objects at runtime.
// Properties added to an ObjectTemplate are added to each object created from the ObjectTemplate.
type ObjectTemplate struct {
*template
}
// NewObjectTemplate creates a new ObjectTemplate.
// The *ObjectTemplate can be used as a v8go.ContextOption to create a global object in a Context.
func NewObjectTemplate(iso *Isolate) *ObjectTemplate {
if iso == nil {
panic("nil Isolate argument not supported")
}
tmpl := &template{
ptr: C.NewObjectTemplate(iso.ptr),
iso: iso,
}
runtime.SetFinalizer(tmpl, (*template).finalizer)
return &ObjectTemplate{tmpl}
}
// NewInstance creates a new Object based on the template.
func (o *ObjectTemplate) NewInstance(ctx *Context) (*Object, error) {
if ctx == nil {
return nil, errors.New("v8go: Context cannot be <nil>")
}
rtn := C.ObjectTemplateNewInstance(o.ptr, ctx.ptr)
runtime.KeepAlive(o)
return objectResult(ctx, rtn)
}
// SetInternalFieldCount sets the number of internal fields that instances of this
// template will have.
func (o *ObjectTemplate) SetInternalFieldCount(fieldCount uint32) {
C.ObjectTemplateSetInternalFieldCount(o.ptr, C.int(fieldCount))
}
// InternalFieldCount returns the number of internal fields that instances of this
// template will have.
func (o *ObjectTemplate) InternalFieldCount() uint32 {
return uint32(C.ObjectTemplateInternalFieldCount(o.ptr))
}
func (o *ObjectTemplate) apply(opts *contextOptions) {
opts.gTmpl = o
}