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
9 ERROR: javascriptcore-error error ;
13 : with-global-context ( quot -- )
15 [ f JSGlobalContextCreate dup js-context set ] dip
17 [ drop '[ _ JSGlobalContextRelease ] ] 2bi
21 : with-javascriptcore ( quot -- )
23 with-global-context ; inline
25 : JSString>string ( JSString -- string )
26 dup JSStringGetMaximumUTF8CStringSize [ <byte-array> ] keep
27 [ JSStringGetUTF8CString drop ] [ drop ] 2bi
28 utf8 decode [ 0 = ] trim-tail ;
30 : JSValueRef>string ( ctx JSValueRef/f -- string/f )
33 [ JSString>string ] [ JSStringRelease ] bi
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 ;
44 : eval-js-standalone ( string -- result-string )
45 '[ _ eval-js ] with-javascriptcore ;
47 : eval-js-path-standalone ( path -- result-string ) utf8 file-contents eval-js-standalone ;