export goClassConstructorProxy
(ctx *C.JSContext, newTarget C.JSValueConst, argc C.int, argv *C.JSValueConst, magic C.int)
| 39 | |
| 40 | //export goClassConstructorProxy |
| 41 | func goClassConstructorProxy(ctx *C.JSContext, newTarget C.JSValueConst, |
| 42 | argc C.int, argv *C.JSValueConst, magic C.int) C.JSValue { |
| 43 | |
| 44 | goCtx, fn, perr := getContextAndObject(ctx, magic, errConstructorNotFound) |
| 45 | if perr != nil { |
| 46 | return throwProxyError(ctx, *perr) |
| 47 | } |
| 48 | |
| 49 | builder, ok := fn.(*ClassBuilder) |
| 50 | if !ok { |
| 51 | return throwProxyError(ctx, errInvalidConstructorType) |
| 52 | } |
| 53 | |
| 54 | classID, exists := getConstructorClassID(goCtx, C.JSValue(newTarget)) |
| 55 | if !exists { |
| 56 | v := &Value{ctx: goCtx, ref: C.JSValue(newTarget)} |
| 57 | classID, exists = v.resolveClassIDFromInheritance() |
| 58 | } |
| 59 | if !exists { |
| 60 | return throwProxyError(ctx, proxyError{"InternalError", "Class ID not found for constructor"}) |
| 61 | } |
| 62 | |
| 63 | var instanceProperties []C.PropertyEntry |
| 64 | var instancePropertyNames []*C.char |
| 65 | type materializedInstanceProperty struct { |
| 66 | spec ValueSpec |
| 67 | value *Value |
| 68 | } |
| 69 | var materializedProperties []materializedInstanceProperty |
| 70 | defer func() { |
| 71 | // bridge.c/BindPropertyToObject duplicates property values with JS_DupValue |
| 72 | // before defining properties on the instance. Free only non-legacy values |
| 73 | // that were materialized for this constructor invocation. |
| 74 | for _, p := range materializedProperties { |
| 75 | if p.value == nil || isContextValueSpec(p.spec) { |
| 76 | continue |
| 77 | } |
| 78 | p.value.Free() |
| 79 | } |
| 80 | }() |
| 81 | defer func() { |
| 82 | for _, cStr := range instancePropertyNames { |
| 83 | C.free(unsafe.Pointer(cStr)) |
| 84 | } |
| 85 | }() |
| 86 | |
| 87 | for _, property := range builder.properties { |
| 88 | if !property.Static { |
| 89 | if property.Spec == nil { |
| 90 | return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("property value is required: %s", property.Name)}) |
| 91 | } |
| 92 | propertyValue, err := materializeValueSpecSafely(goCtx, property.Spec) |
| 93 | if err != nil { |
| 94 | return throwProxyError(ctx, proxyError{"InternalError", fmt.Sprintf("invalid property value: %s (materialize error: %v)", property.Name, err)}) |
| 95 | } |
| 96 | if propertyValue != nil && propertyValue.ctx == goCtx { |
| 97 | materializedProperties = append(materializedProperties, materializedInstanceProperty{spec: property.Spec, value: propertyValue}) |
| 98 | } |
no test coverage detected