]> gitweb.factorcode.org Git - factor.git/blob - unmaintained/javascriptcore/javascriptcore.factor
e3465a324ba3087aaffead9dce9eac660fa2f312
[factor.git] / unmaintained / javascriptcore / javascriptcore.factor
1 ! Copyright (C) 2010 Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: alien.c-types alien.data byte-arrays continuations fry
4 io.encodings.string io.encodings.utf8 io.files
5 javascriptcore.ffi javascriptcore.ffi.hack kernel namespaces
6 sequences ;
7 IN: javascriptcore
8
9 ERROR: javascriptcore-error error ;
10
11 SYMBOL: js-context
12
13 : with-global-context ( quot -- )
14     [
15         [ f JSGlobalContextCreate dup js-context set ] dip
16         [ nip '[ @ ] ]
17         [ drop '[ _ JSGlobalContextRelease ] ] 2bi
18         [ ] cleanup
19     ] with-scope ; inline
20
21 : with-javascriptcore ( quot -- )
22     set-callstack-bounds
23     with-global-context ; inline
24
25 : JSString>string ( JSString -- string )
26     dup JSStringGetMaximumUTF8CStringSize [ <byte-array> ] keep
27     [ JSStringGetUTF8CString drop ] [ drop ] 2bi
28     utf8 decode [ 0 = ] trim-tail ;
29
30 : JSValueRef>string ( ctx JSValueRef/f -- string/f )
31     [
32         f JSValueToStringCopy
33         [ JSString>string ] [ JSStringRelease ] bi
34     ] [
35         drop f
36     ] if* ;
37
38 : eval-js ( string -- result-string )
39     [ js-context get dup ] dip
40     JSStringCreateWithUTF8CString f f 0
41     { { void* initial: f } } [ JSEvaluateScript ] with-out-parameters
42     dup [ nip JSValueRef>string javascriptcore-error ] [ drop JSValueRef>string ] if ;
43
44 : eval-js-standalone ( string -- result-string )
45     '[ _ eval-js ] with-javascriptcore ;
46
47 : eval-js-path-standalone ( path -- result-string ) utf8 file-contents eval-js-standalone ;
48