-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathclass_bridge.go
More file actions
210 lines (185 loc) · 5.76 KB
/
class_bridge.go
File metadata and controls
210 lines (185 loc) · 5.76 KB
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package quickjs
import (
"fmt"
"unsafe"
)
/*
#include <stdint.h>
#include "bridge.h"
*/
import "C"
func classObjectOpaque(objectID int32) unsafe.Pointer {
if objectID >= 0 {
return nil
}
return C.IntToOpaque(C.int32_t(objectID))
}
func resolveClassObjectFromOpaque(ctx *Context, opaque unsafe.Pointer) (*Context, int32, bool) {
if ctx == nil || ctx.runtime == nil || opaque == nil {
return nil, 0, false
}
objectID := int32(C.OpaqueToInt(opaque))
if objectID < 0 {
identity, ok := ctx.runtime.getClassObjectIdentity(objectID)
if ok {
ownerCtx := ctx.runtime.getOwnedContextByID(identity.contextID)
if ownerCtx == nil || ownerCtx.handleStore == nil {
return nil, 0, false
}
return ownerCtx, identity.handleID, true
}
}
return nil, 0, false
}
//export goClassConstructorProxy
func goClassConstructorProxy(ctx *C.JSContext, newTarget C.JSValueConst,
argc C.int, argv *C.JSValueConst, magic C.int) C.JSValue {
goCtx, fn, perr := getContextAndObject(ctx, magic, errConstructorNotFound)
if perr != nil {
return throwProxyError(ctx, *perr)
}
builder, ok := fn.(*ClassBuilder)
if !ok {
return throwProxyError(ctx, errInvalidConstructorType)
}
classID, exists := getConstructorClassID(goCtx, C.JSValue(newTarget))
if !exists {
v := &Value{ctx: goCtx, ref: C.JSValue(newTarget)}
classID, exists = v.resolveClassIDFromInheritance()
}
if !exists {
return throwProxyError(ctx, proxyError{"InternalError", "Class ID not found for constructor"})
}
var instanceProperties []C.PropertyEntry
var instancePropertyNames []*C.char
type materializedInstanceProperty struct {
spec ValueSpec
value *Value
}
var materializedProperties []materializedInstanceProperty
defer func() {
// bridge.c/BindPropertyToObject duplicates property values with JS_DupValue
// before defining properties on the instance. Free only non-legacy values
// that were materialized for this constructor invocation.
for _, p := range materializedProperties {
if p.value == nil || isContextValueSpec(p.spec) {
continue
}
p.value.Free()
}
}()
defer func() {
for _, cStr := range instancePropertyNames {
C.free(unsafe.Pointer(cStr))
}
}()
for _, property := range builder.properties {
if !property.Static {
if property.Spec == nil {
return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("property value is required: %s", property.Name)})
}
propertyValue, err := materializeValueSpecSafely(goCtx, property.Spec)
if err != nil {
return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("invalid property value: %s (materialize error: %v)", property.Name, err)})
}
if propertyValue != nil && propertyValue.ctx == goCtx {
materializedProperties = append(materializedProperties, materializedInstanceProperty{spec: property.Spec, value: propertyValue})
}
if propertyValue == nil {
return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("invalid property value: %s (materialize returned nil)", property.Name)})
}
if !propertyValue.belongsTo(goCtx) {
return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("invalid property value: %s (materialized in a different context)", property.Name)})
}
propertyName := C.CString(property.Name)
instancePropertyNames = append(instancePropertyNames, propertyName)
instanceProperties = append(instanceProperties, C.PropertyEntry{
name: propertyName,
value: propertyValue.ref,
is_static: C.int(0),
flags: C.int(property.Flags),
})
}
}
var instancePropertiesPtr *C.PropertyEntry
if len(instanceProperties) > 0 {
instancePropertiesPtr = &instanceProperties[0]
}
instance := C.CreateClassInstance(
ctx,
C.JSValue(newTarget),
C.JSClassID(classID),
instancePropertiesPtr,
C.int(len(instanceProperties)),
)
if bool(C.JS_IsException(instance)) {
return instance
}
instanceValue := &Value{ctx: goCtx, ref: instance}
args := convertCArgsToGoValues(argc, argv, goCtx)
goObj, err := builder.constructor(goCtx, instanceValue, args)
if err != nil {
C.JS_FreeValue(ctx, instance)
errorMsg := C.CString(err.Error())
defer C.free(unsafe.Pointer(errorMsg))
return C.ThrowInternalError(ctx, errorMsg)
}
if goObj != nil {
if goCtx.handleStore == nil {
C.JS_FreeValue(ctx, instance)
return throwProxyError(ctx, proxyError{"InternalError", "Handle store not available"})
}
if goCtx.runtime == nil {
C.JS_FreeValue(ctx, instance)
return throwProxyError(ctx, proxyError{"InternalError", "Context runtime not available"})
}
handleID := goCtx.handleStore.Store(goObj)
objectID := goCtx.runtime.registerClassObjectIdentity(goCtx.contextID, handleID)
if objectID == 0 {
goCtx.handleStore.Delete(handleID)
C.JS_FreeValue(ctx, instance)
return throwProxyError(ctx, proxyError{"InternalError", "Failed to register class object identity"})
}
C.JS_SetOpaque(instance, classObjectOpaque(objectID))
}
return instance
}
//export goClassFinalizerProxy
func goClassFinalizerProxy(rt *C.JSRuntime, val C.JSValue) {
goRt := getRuntimeFromJS(rt)
if goRt == nil {
return
}
classID := C.JS_GetClassID(val)
opaque := C.JS_GetOpaque(val, classID)
if opaque == nil {
return
}
objectID := int32(C.OpaqueToInt(opaque))
var targetCtx *Context
var handleID int32
if objectID < 0 {
identity, ok := goRt.takeClassObjectIdentity(objectID)
if !ok {
return
}
targetCtx = goRt.getOwnedContextByID(identity.contextID)
handleID = identity.handleID
} else {
return
}
if targetCtx == nil || targetCtx.handleStore == nil {
return
}
if goObj, exists := targetCtx.handleStore.Load(handleID); exists {
if finalizer, ok := goObj.(ClassFinalizer); ok {
func() {
defer func() {
recover()
}()
finalizer.Finalize()
}()
}
targetCtx.handleStore.Delete(handleID)
}
}