about summary refs log tree commit diff stats
path: root/src/js/javascript.nim
diff options
context:
space:
mode:
Diffstat (limited to 'src/js/javascript.nim')
-rw-r--r--src/js/javascript.nim108
1 files changed, 108 insertions, 0 deletions
diff --git a/src/js/javascript.nim b/src/js/javascript.nim
new file mode 100644
index 00000000..2518f252
--- /dev/null
+++ b/src/js/javascript.nim
@@ -0,0 +1,108 @@
+import options
+
+import bindings/quickjs
+
+export
+  JS_NULL, JS_UNDEFINED, JS_FALSE, JS_TRUE, JS_EXCEPTION, JS_UNINITIALIZED
+
+export
+  JS_EVAL_TYPE_GLOBAL,
+  JS_EVAL_TYPE_MODULE,
+  JS_EVAL_TYPE_DIRECT,
+  JS_EVAL_TYPE_INDIRECT,
+  JS_EVAL_TYPE_MASK,
+  JS_EVAL_FLAG_SHEBANG,
+  JS_EVAL_FLAG_STRICT,
+  JS_EVAL_FLAG_STRIP,
+  JS_EVAL_FLAG_COMPILE_ONLY
+
+export JSRuntime, JSContext, JSValue
+
+type
+  JSObject* = object
+    ctx*: JSContext
+    val*: JSValue
+
+  JSContextOpaque* = object
+    err*: string
+
+func newJSRuntime*(): JSRuntime =
+  result = JS_NewRuntime()
+
+proc newJSContext*(rt: JSRuntime): JSContext =
+  result = JS_NewContext(rt)
+  let opaque = cast[ptr JSContextOpaque](alloc0(sizeof(JSContextOpaque)))
+  opaque.err = ""
+  JS_SetContextOpaque(result, opaque)
+
+func getJSObject*(ctx: JSContext, v: JSValue): JSObject =
+  result.ctx = ctx
+  result.val = v
+
+func getJSObject*(ctx: JSContext, argv: ptr JSValue, i: int): JSObject =
+  getJSObject(ctx, cast[ptr JSValue](cast[int](argv) + i * sizeof(JSValue))[])
+
+func newJSObject*(ctx: JSContext): JSObject =
+  result.ctx = ctx
+  result.val = JS_NewObject(ctx)
+
+func newJSCFunction*(ctx: JSContext, name: string, fun: JSCFunction, argc: int): JSObject =
+  result.ctx = ctx
+  result.val = JS_NewCFunction(ctx, fun, cstring(name), argc)
+
+func getGlobalObject*(ctx: JSContext): JSObject =
+  result.ctx = ctx
+  result.val = JS_GetGlobalObject(ctx)
+
+func getException*(ctx: JSContext): JSObject =
+  result.ctx = ctx
+  result.val = JS_GetException(ctx)
+
+func getProperty*(obj: JSObject, s: string): JSObject =
+  result.ctx = obj.ctx
+  result.val = JS_GetPropertyStr(obj.ctx, obj.val, cstring(s));
+
+func getOpaque*(ctx: JSContext): ptr JSContextOpaque =
+  return cast[ptr JSContextOpaque](JS_GetContextOpaque(ctx))
+
+func toString*(obj: JSObject): Option[string] =
+  var plen: int
+  let outp = JS_ToCStringLen(obj.ctx, addr plen, obj.val) # cstring
+  if outp != nil:
+    var ret = newString(plen)
+    for i in 0..<plen:
+      ret[i] = outp[i]
+    result = some(ret)
+    JS_FreeCString(obj.ctx, outp) # refc
+
+func `$`*(obj: JSObject): string =
+  return obj.toString().get("")
+
+func isUndefined*(obj: JSObject): bool = JS_IsUndefined(obj.val)
+func isException*(obj: JSObject): bool = JS_IsException(obj.val)
+
+proc setProperty*(obj: JSObject, name: string, prop: JSObject) =
+  discard JS_SetPropertyStr(obj.ctx, obj.val, cstring(name), prop.val)
+
+proc setFunctionProperty*(obj: JSObject, name: string, fun: JSCFunction) =
+  obj.setProperty(name, obj.ctx.newJSCFunction(name, fun, 1))
+  #  (proc(ctx: JSContext, obj: JSValue, argc: int, argv: ptr JSValue): JSValue {.cdecl.} =
+  #    var invoc: seq[JSValue]
+  #    for i in 0..<argc:
+  #      let arg = cast[ptr JSValue](cast[int](argv) + i * sizeof(JSValue))[]
+  #      invoc.add(arg)
+  #    return fun(JSObject(ctx: ctx, qjs: obj), invoc)
+  #), cstring(name), 1))
+
+proc free*(ctx: JSContext) =
+  JS_FreeContext(ctx)
+
+proc free*(rt: JSRuntime) =
+  JS_FreeRuntime(rt)
+
+proc free*(obj: JSObject) =
+  JS_FreeValue(obj.ctx, obj.val)
+
+proc eval*(ctx: JSContext, s: string, file: string, eval_flags: int): JSObject =
+  result.ctx = ctx
+  result.val = JS_Eval(ctx, cstring(s), s.len, cstring(file), eval_flags)
e='Blame the previous revision' href='/ingrix/lynx-snapshots/blame/src/HTAlert.h?id=e087f6d44e87f489fcb3056e86319ebba4218156'>^
e087f6d4


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