forked from rogchap/v8go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
object_template.go
64 lines (54 loc) · 1.76 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
// 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, error) {
if iso == nil {
return nil, errors.New("v8go: failed to create new ObjectTemplate: Isolate cannot be <nil>")
}
tmpl := &template{
ptr: C.NewObjectTemplate(iso.ptr),
iso: iso,
}
runtime.SetFinalizer(tmpl, (*template).finalizer)
return &ObjectTemplate{tmpl}, nil
}
// 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>")
}
valPtr := C.ObjectTemplateNewInstance(o.ptr, ctx.ptr)
return &Object{&Value{valPtr, ctx}}, nil
}
func (o *ObjectTemplate) apply(opts *contextOptions) {
opts.gTmpl = o
}