-
Notifications
You must be signed in to change notification settings - Fork 2
/
module.v
108 lines (98 loc) · 2.14 KB
/
module.v
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
module vjs
type JSModuleInitFunc = fn (&C.JSContext, &C.JSModuleDef) int
fn C.JS_NewCModule(&C.JSContext, &char, &JSModuleInitFunc) &C.JSModuleDef
fn C.JS_SetModuleExport(&C.JSContext, &C.JSModuleDef, &char, C.JSValue) int
fn C.JS_AddModuleExport(&C.JSContext, &C.JSModuleDef, &char) int
// Module structure.
pub struct Module {
ctx Context
name string
mut:
exports_str []string
exports []&char
values []Value
}
// Initial `js_module`.
// Example:
// ```v
// mod := ctx.js_module('my-module')
// ```
pub fn (ctx &Context) js_module(name string) Module {
return Module{
ctx: ctx
name: name
}
}
// Export module.
// Example:
// ```v
// mod.export('foo', 'bar')
// ```
@[manualfree]
pub fn (mut m Module) export(name string, any AnyValue) {
ptr := name.str
m.exports_str << name
m.exports << ptr
m.values << m.ctx.any_to_val(any)
unsafe {
free(ptr)
}
}
// Same as Export.
pub fn (mut m Module) set(name string, any AnyValue) {
m.export(name, any)
}
// Get value from export/set.
pub fn (mut m Module) get(name string) Value {
mut val := m.ctx.js_undefined()
len := m.exports_str.len
for i in 0 .. len {
str := m.exports_str[i]
if str == name {
val = m.values[i]
break
}
}
return val
}
// Convert module to JS object.
pub fn (mut m Module) to_object() Value {
obj := m.ctx.js_object()
len := m.exports_str.len
for i in 0 .. len {
obj.set(m.exports_str[i], m.values[i])
}
return obj
}
// Export default.
// Example:
// ```v
// mod.export_default(mod.to_object())
// ```
pub fn (mut m Module) export_default(any AnyValue) {
m.export('default', any)
}
// Create module.
// Example:
// ```v
// mod := ctx.js_module('my-module')
// mod.export('foo', 'bar')
// mod.export_default(mod.to_object())
// mod.create()
// ```
pub fn (mut m Module) create() &C.JSModuleDef {
cb := fn [m] (ctx &C.JSContext, mod &C.JSModuleDef) int {
len := m.exports.len
for i in 0 .. len {
export := m.exports[i]
value := m.values[i]
C.JS_SetModuleExport(ctx, mod, export, value.ref)
}
return len
}
ref := C.JS_NewCModule(m.ctx.ref, m.name.str, cb)
for export in m.exports {
C.JS_AddModuleExport(m.ctx.ref, ref, export)
}
return ref
}