Merge branch 'master' of factorcode.org:/git/factor

db4
Joe Groff 2010-04-20 13:51:33 -07:00
commit d6b56c7e25
3 changed files with 55 additions and 4 deletions

View File

@ -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 ) ;

View File

@ -0,0 +1,10 @@
! Copyright (C) 2010 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors javascriptcore kernel tools.test ;
IN: javascriptcore.tests
[ "2" ] [ "1+1" eval-js ] unit-test
[ "1+shoes" eval-js ]
[ error>> "ReferenceError: Can't find variable: shoes" = ] must-fail-with

View File

@ -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
ERROR: javascriptcore-error error ;
: 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 -- result-string )
'[
[
dup _ JSStringCreateWithUTF8CString f f 0 JSValueRef <c-object>
[ JSEvaluateScript ] keep *void*
dup [ nip JSValueRef>string javascriptcore-error ] [ drop JSValueRef>string ] if
] with-global-context
] with-javascriptcore ;
: eval-js-path ( path -- result-string ) utf8 file-contents eval-js ;