-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy patheval.go
More file actions
73 lines (50 loc) · 1.54 KB
/
eval.go
File metadata and controls
73 lines (50 loc) · 1.54 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
package qjs
func load(c *Context, file string, flags ...EvalOptionFunc) (*Value, error) {
if file == "" {
return nil, ErrInvalidFileName
}
// Module: Force TypeModule() since load only works with modules
flags = append(flags, TypeModule())
option := createEvalOption(c, file, flags...)
evalOptions := option.Handle()
defer option.Free()
result := c.Call("QJS_Load", c.Raw(), evalOptions)
return normalizeJsValue(c, result)
}
func eval(c *Context, file string, flags ...EvalOptionFunc) (*Value, error) {
if file == "" {
return nil, ErrInvalidFileName
}
option := createEvalOption(c, file, flags...)
evalOptions := option.Handle()
defer option.Free()
result := c.Call("QJS_Eval", c.Raw(), evalOptions)
return normalizeJsValue(c, result)
}
func compile(c *Context, file string, flags ...EvalOptionFunc) (_ []byte, err error) {
option := createEvalOption(c, file, flags...)
evalOptions := option.Handle()
defer option.Free()
result := c.Call("QJS_Compile2", c.Raw(), evalOptions)
if result, err = normalizeJsValue(c, result); err != nil {
return nil, err
}
defer result.Free()
bytecodeBytes := result.Bytes()
// Bytecode: Create independent copy to avoid memory corruption
bytes := make([]byte, len(bytecodeBytes))
copy(bytes, bytecodeBytes)
return bytes, nil
}
func normalizeJsValue(c *Context, value *Value) (*Value, error) {
hasException := c.HasException()
if hasException {
value.Free()
return nil, c.Exception()
}
if value.IsError() {
defer value.Free()
return nil, value.Exception()
}
return value, nil
}