Add unit test to javascriptcore, make eval-js throw errors and return a string

db4
Doug Coleman 2010-04-20 14:21:05 -05:00
parent e4b67b268c
commit 73eb31a35c
2 changed files with 18 additions and 8 deletions

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

@ -6,6 +6,8 @@ javascriptcore.ffi javascriptcore.ffi.hack kernel namespaces
sequences ; sequences ;
IN: javascriptcore IN: javascriptcore
ERROR: javascriptcore-error error ;
: with-javascriptcore ( quot -- ) : with-javascriptcore ( quot -- )
set-callstack-bounds set-callstack-bounds
call ; inline call ; inline
@ -33,16 +35,14 @@ SYMBOL: js-context
drop f drop f
] if* ; ] if* ;
: eval-js ( string -- ret/f exception/f ) : eval-js ( string -- result-string )
'[
[ [
[ dup _ JSStringCreateWithUTF8CString f f 0 JSValueRef <c-object>
[
swap JSStringCreateWithUTF8CString f f 0 JSValueRef <c-object>
[ JSEvaluateScript ] keep *void* [ JSEvaluateScript ] keep *void*
] dup [ nip JSValueRef>string javascriptcore-error ] [ drop JSValueRef>string ] if
[ '[ [ _ ] dip JSValueRef>string ] bi@ ] bi
] with-global-context ] with-global-context
] with-javascriptcore ; ] with-javascriptcore ;
: eval-js-path ( path -- ret/f exception/f ) utf8 file-contents eval-js ; : eval-js-path ( path -- result-string ) utf8 file-contents eval-js ;