export goModuleInitProxy
(ctx *C.JSContext, m *C.JSModuleDef)
| 49 | |
| 50 | //export goModuleInitProxy |
| 51 | func goModuleInitProxy(ctx *C.JSContext, m *C.JSModuleDef) C.int { |
| 52 | goCtx, builder, builderID, err := getContextAndModuleBuilder(ctx, m) |
| 53 | if err != nil { |
| 54 | return throwModuleError(ctx, err) |
| 55 | } |
| 56 | defer cleanupModuleBuilderHandle(goCtx, builderID) |
| 57 | |
| 58 | type materializedModuleExport struct { |
| 59 | spec ValueSpec |
| 60 | value *Value |
| 61 | } |
| 62 | var materializedExports []materializedModuleExport |
| 63 | defer func() { |
| 64 | for _, item := range materializedExports { |
| 65 | if item.value == nil || item.value.ctx != goCtx || isContextValueSpec(item.spec) { |
| 66 | continue |
| 67 | } |
| 68 | item.value.Free() |
| 69 | } |
| 70 | }() |
| 71 | |
| 72 | for _, export := range builder.exports { |
| 73 | if export.Spec == nil { |
| 74 | return throwModuleError(ctx, fmt.Errorf("invalid module export value: %s", export.Name)) |
| 75 | } |
| 76 | |
| 77 | value, matErr := materializeValueSpecSafely(goCtx, export.Spec) |
| 78 | if matErr != nil { |
| 79 | return throwModuleError(ctx, fmt.Errorf("invalid module export value: %s (materialize error: %v)", export.Name, matErr)) |
| 80 | } |
| 81 | if value != nil && value.ctx == goCtx { |
| 82 | materializedExports = append(materializedExports, materializedModuleExport{spec: export.Spec, value: value}) |
| 83 | } |
| 84 | if value == nil { |
| 85 | return throwModuleError(ctx, fmt.Errorf("invalid module export value: %s (materialize returned nil)", export.Name)) |
| 86 | } |
| 87 | if !value.belongsTo(goCtx) { |
| 88 | return throwModuleError(ctx, fmt.Errorf("invalid module export value: %s (materialized in a different context)", export.Name)) |
| 89 | } |
| 90 | |
| 91 | exportName := C.CString(export.Name) |
| 92 | legacySpec := isContextValueSpec(export.Spec) |
| 93 | rc := C.JS_SetModuleExport(ctx, m, exportName, value.ref) |
| 94 | C.free(unsafe.Pointer(exportName)) |
| 95 | if rc < 0 { |
| 96 | // JS_SetModuleExport frees val on failure, so source handles |
| 97 | // must be invalidated to avoid a later double free. |
| 98 | value.ref = C.JS_NewUndefined() |
| 99 | value.borrowed = false |
| 100 | return throwModuleError(ctx, fmt.Errorf("failed to set module export: %s", export.Name)) |
| 101 | } |
| 102 | if legacySpec { |
| 103 | // Legacy Export(name, *Value) now keeps source values readable after Build. |
| 104 | // JS_SetModuleExport consumes the original ref, so mark this Go value as a |
| 105 | // borrowed non-owning alias. Lifetime is held by the module export slot, |
| 106 | // and Free only invalidates the Go wrapper without decref. |
| 107 | value.borrowed = true |
| 108 | } else { |
no test coverage detected