Add eval-js and eval-js-file
parent
366ce2896f
commit
e4b67b268c
|
@ -1,7 +1,8 @@
|
|||
! Copyright (C) 2010 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: alien alien.c-types alien.libraries alien.syntax
|
||||
classes.struct combinators io.encodings.utf8 system ;
|
||||
classes.struct combinators io.encodings.utf16n
|
||||
io.encodings.utf8 system ;
|
||||
IN: javascriptcore.ffi
|
||||
|
||||
<<
|
||||
|
@ -9,6 +10,7 @@ IN: javascriptcore.ffi
|
|||
{ [ os macosx? ] [ "/System/Library/Frameworks/JavaScriptCore.framework/Versions/Current/JavaScriptCore" ] }
|
||||
! { [ os winnt? ] [ "javascriptcore.dll" ] }
|
||||
! { [ os unix? ] [ "libsqlite3.so" ] }
|
||||
[ ]
|
||||
} cond cdecl add-library
|
||||
>>
|
||||
|
||||
|
@ -36,7 +38,6 @@ TYPEDEF: void* JSObjectHasInstanceCallback
|
|||
TYPEDEF: void* JSObjectConvertToTypeCallback
|
||||
TYPEDEF: uint unsigned
|
||||
TYPEDEF: ushort JSChar
|
||||
! char[utf16n] for strings
|
||||
|
||||
C-ENUM: JSPropertyAttributes
|
||||
{ kJSPropertyAttributeNone 0 }
|
||||
|
@ -202,7 +203,7 @@ FUNCTION: void JSPropertyNameAccumulatorAddName ( JSPropertyNameAccumulatorRef a
|
|||
|
||||
FUNCTION: JSStringRef JSStringCreateWithCharacters ( JSChar* chars, size_t numChars ) ;
|
||||
|
||||
FUNCTION: JSStringRef JSStringCreateWithUTF8CString ( c-string[utf8] string ) ;
|
||||
FUNCTION: JSStringRef JSStringCreateWithUTF8CString ( c-string string ) ;
|
||||
|
||||
FUNCTION: JSStringRef JSStringRetain ( JSStringRef string ) ;
|
||||
|
||||
|
|
|
@ -1,8 +1,48 @@
|
|||
! Copyright (C) 2010 Doug Coleman.
|
||||
! See http://factorcode.org/license.txt for BSD license.
|
||||
USING: javascriptcore.ffi.hack kernel ;
|
||||
USING: alien.c-types alien.data byte-arrays continuations fry
|
||||
io.encodings.string io.encodings.utf8 io.files
|
||||
javascriptcore.ffi javascriptcore.ffi.hack kernel namespaces
|
||||
sequences ;
|
||||
IN: javascriptcore
|
||||
|
||||
: with-javascriptcore ( quot -- )
|
||||
set-callstack-bounds
|
||||
call ; inline
|
||||
|
||||
SYMBOL: js-context
|
||||
|
||||
: with-global-context ( quot -- )
|
||||
[
|
||||
[ f JSGlobalContextCreate ] dip
|
||||
[ '[ _ @ ] ]
|
||||
[ drop '[ _ JSGlobalContextRelease ] ] 2bi
|
||||
[ ] cleanup
|
||||
] with-scope ; inline
|
||||
|
||||
: JSString>string ( JSString -- string )
|
||||
dup JSStringGetMaximumUTF8CStringSize [ <byte-array> ] keep
|
||||
[ JSStringGetUTF8CString drop ] [ drop ] 2bi
|
||||
utf8 decode [ 0 = ] trim-tail ;
|
||||
|
||||
: JSValueRef>string ( ctx JSValueRef/f -- string/f )
|
||||
[
|
||||
f JSValueToStringCopy
|
||||
[ JSString>string ] [ JSStringRelease ] bi
|
||||
] [
|
||||
drop f
|
||||
] if* ;
|
||||
|
||||
: eval-js ( string -- ret/f exception/f )
|
||||
[
|
||||
[
|
||||
[
|
||||
swap JSStringCreateWithUTF8CString f f 0 JSValueRef <c-object>
|
||||
[ JSEvaluateScript ] keep *void*
|
||||
]
|
||||
[ '[ [ _ ] dip JSValueRef>string ] bi@ ] bi
|
||||
] with-global-context
|
||||
] with-javascriptcore ;
|
||||
|
||||
: eval-js-path ( path -- ret/f exception/f ) utf8 file-contents eval-js ;
|
||||
|
||||
|
|
Loading…
Reference in New Issue