SpiderMonkey in FB

Windows specific questions.
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

SpiderMonkey in FB

Postby porfirio » May 04, 2006 17:45

Hi all.

SpiderMonkey (JavaScript-C) Engine

I was searching arround about how to port SpyderMonkey to work in FB

SpyderMonkey is C and its compiled to js32.dll , what i need is to convert the headers, and i am a bit losted

Anyone can help??
yetifoot
Posts: 1710
Joined: Sep 11, 2005 7:08
Location: England
Contact:

Postby yetifoot » May 05, 2006 11:16

If you search the forum for SWIG, you will find a program that can be useful to convert c headers. There should be some topics involving how to use it too. Some may need to be hand translated, if you get stuck on a particular bit post again with specifics, and hopefully someone will be able to help.
voodooattack
Posts: 605
Joined: Feb 18, 2006 13:30
Location: Alexandria / Egypt
Contact:

Postby voodooattack » May 05, 2006 12:49

Hi! :)

I've just got the source and managed to build it..
looks neat..

the only prob is.. the headers are totally f**ked up.. :(

here's a small example:

Code: Select all

JS_BEGIN_EXTERN_C

/*
 * Type tags stored in the low bits of a jsval.
 */
#define JSVAL_OBJECT            0x0     /* untagged reference to object */
#define JSVAL_INT               0x1     /* tagged 31-bit integer value */
#define JSVAL_DOUBLE            0x2     /* tagged reference to double */
#define JSVAL_STRING            0x4     /* tagged reference to string */
#define JSVAL_BOOLEAN           0x6     /* tagged boolean value */

/* Type tag bitfield length and derived macros. */
#define JSVAL_TAGBITS           3
#define JSVAL_TAGMASK           JS_BITMASK(JSVAL_TAGBITS)
#define JSVAL_TAG(v)            ((v) & JSVAL_TAGMASK)
#define JSVAL_SETTAG(v,t)       ((v) | (t))
#define JSVAL_CLRTAG(v)         ((v) & ~(jsval)JSVAL_TAGMASK)
#define JSVAL_ALIGN             JS_BIT(JSVAL_TAGBITS)

/* Predicates for type testing. */
#define JSVAL_IS_OBJECT(v)      (JSVAL_TAG(v) == JSVAL_OBJECT)
#define JSVAL_IS_NUMBER(v)      (JSVAL_IS_INT(v) || JSVAL_IS_DOUBLE(v))
#define JSVAL_IS_INT(v)         (((v) & JSVAL_INT) && (v) != JSVAL_VOID)
#define JSVAL_IS_DOUBLE(v)      (JSVAL_TAG(v) == JSVAL_DOUBLE)
#define JSVAL_IS_STRING(v)      (JSVAL_TAG(v) == JSVAL_STRING)
#define JSVAL_IS_BOOLEAN(v)     (JSVAL_TAG(v) == JSVAL_BOOLEAN)
#define JSVAL_IS_NULL(v)        ((v) == JSVAL_NULL)
#define JSVAL_IS_VOID(v)        ((v) == JSVAL_VOID)
#define JSVAL_IS_PRIMITIVE(v)   (!JSVAL_IS_OBJECT(v) || JSVAL_IS_NULL(v))

/* Objects, strings, and doubles are GC'ed. */
#define JSVAL_IS_GCTHING(v)     (!((v) & JSVAL_INT) && !JSVAL_IS_BOOLEAN(v))
#define JSVAL_TO_GCTHING(v)     ((void *)JSVAL_CLRTAG(v))
#define JSVAL_TO_OBJECT(v)      ((JSObject *)JSVAL_TO_GCTHING(v))
#define JSVAL_TO_DOUBLE(v)      ((jsdouble *)JSVAL_TO_GCTHING(v))
#define JSVAL_TO_STRING(v)      ((JSString *)JSVAL_TO_GCTHING(v))
#define OBJECT_TO_JSVAL(obj)    ((jsval)(obj))
#define DOUBLE_TO_JSVAL(dp)     JSVAL_SETTAG((jsval)(dp), JSVAL_DOUBLE)
#define STRING_TO_JSVAL(str)    JSVAL_SETTAG((jsval)(str), JSVAL_STRING)

/* Lock and unlock the GC thing held by a jsval. */
#define JSVAL_LOCK(cx,v)        (JSVAL_IS_GCTHING(v)                          \
                                 ? JS_LockGCThing(cx, JSVAL_TO_GCTHING(v))    \
                                 : JS_TRUE)
#define JSVAL_UNLOCK(cx,v)      (JSVAL_IS_GCTHING(v)                          \
                                 ? JS_UnlockGCThing(cx, JSVAL_TO_GCTHING(v))  \
                                 : JS_TRUE)

/* Domain limits for the jsval int type. */
#define JSVAL_INT_BITS          31
#define JSVAL_INT_POW2(n)       ((jsval)1 << (n))
#define JSVAL_INT_MIN           ((jsval)1 - JSVAL_INT_POW2(30))
#define JSVAL_INT_MAX           (JSVAL_INT_POW2(30) - 1)
#define INT_FITS_IN_JSVAL(i)    ((jsuint)((i)+JSVAL_INT_MAX) <= 2*JSVAL_INT_MAX)
#define JSVAL_TO_INT(v)         ((jsint)(v) >> 1)
#define INT_TO_JSVAL(i)         (((jsval)(i) << 1) | JSVAL_INT)



and here's my favorite:
#ifdef NAME_ALL_GC_ROOTS
#define JS_DEFINE_TO_TOKEN(def) #def
#define JS_DEFINE_TO_STRING(def) JS_DEFINE_TO_TOKEN(def)
#define JS_AddRoot(cx,rp) JS_AddNamedRoot((cx), (rp), (__FILE__ ":" JS_TOKEN_TO_STRING(__LINE__))
#endif


wth?!?! thats like 4 inline functions... i don't think the FB SWIG wrapper can do that, and neither can i...

thats just inhuman..

how could they do that... :-/

I love wrapping stuff to FB.. but not that kinda stuff :P

those headers will take a complete rewrite in FB..

anyways, i think i'll give it a try when i've got more time and see the results.. :-D
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » May 05, 2006 16:47

Besides the conditionals that can be changed to IIF, the type castings to CAST() and the operators, the FB pre-processor also supports stringize (the #symbol) like in C, __FILE__ and __LINE__ too, pretty much copy & paste on that case.
VirusScanner
Posts: 775
Joined: Jul 01, 2005 18:45

Postby VirusScanner » May 05, 2006 22:42

I think I remember seeing somewhere that C allows things like "Hello" " world" since you can't use the + operator with strings, so __FILE__ ":" #__LINE__ is the same as (in FB) __FILE__ + ":" + str(__LINE__)
correct me if I'm wrong though.
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » May 06, 2006 0:16

The latest 0.16 release also supports that, literal strings can be concatenated without + or &, to support some macro tricks, but in that case it must be done the C-way, using a helper macro to do the stringize.

But yeah, str( __LINE__ ) would do the job, STR(expr) will convert expr at compile-time if it's a constant expression.
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

Postby porfirio » May 06, 2006 8:48

Wow!

If is hard for you guys, is impossible to me :p

Looks like i have to forget the ideas that i have for do with this.

If some of you get any sussecefull result please say :)
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » May 06, 2006 18:34

There is it: http://www.freebasic.net/forum/viewtopic.php?p=32815#32815.

I didn't try creating callbacks or anything like that. The latest 0.16 will be needed, plus the lib C headers (that are out-of-date in the CVS repository because the sf.net problems).
Robert Claypool
Posts: 46
Joined: Nov 15, 2005 2:24
Contact:

Helping Mozilla

Postby Robert Claypool » May 08, 2006 9:04

Is there a Mozilla feature request that could be made out of this?
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

Postby porfirio » May 08, 2006 14:22

Problem creating functions :(
Runing that code i get the messagebox and then Windows Error Reporting Dialog saying that the program caused a error .. bla bla bla
if i comment "MessageBox(0,"aha","MessageBox",MB_OK)" and uncoment the others, it wont show nothing!

I must be doing something wrong
jsx.bi

Code: Select all

function alert(cx as JSContext ptr,obj as JSObject ptr,a as JSVAL ptr)as JSINT ptr
   'Dim as zstring ptr b=JS_ValueToString(cx,a)
   'MessageBox(0,b,"MessageBox",MB_OK)
   MessageBox(0,a,"MessageBox",1)
   return 0
end function


function printError(byval cx as JSContext ptr, byval message as zstring ptr,byval report as JSErrorReport ptr)
   Dim as integer where
   'Dim as zstring ptr cstr;
   Dim as string rep,BR
   BR=Chr(13)+Chr(10)
   rep= "JSERROR: "+BR
   If report->filename then
      ? report->filename
   endif
   ? report->lineno
   ? message
   ? " Flags:"
   if (JSREPORT_IS_WARNING(report->flags))then
      ? " WARNING"
   endif
   if (JSREPORT_IS_EXCEPTION(report->flags))then
      ? " EXCEPTION"
   endif
   if (JSREPORT_IS_STRICT(report->flags))then
      ? " STRICT"
   endif
   ? " Error number: "+Str(report->errorNumber)
   'printJSException(cx)
End function

jsx.bas

Code: Select all

option explicit
#include "inc/jsapi.bi"
#include "windows.bi"
#include "JSX.bi"

Dim as JSContext ptr cx    =allocate( 255 )
Dim as JSRuntime ptr rt    =allocate( 255 )
Dim as JSObject  ptr global=allocate( 255 )

static as JSClass global_class = ( _
   @"global", 0, _
   @JS_PropertyStub, @JS_PropertyStub, @JS_PropertyStub, @JS_PropertyStub, _
   @JS_EnumerateStub, @JS_ResolveStub, @JS_ConvertStub, @JS_FinalizeStub  _
)

rt = JS_NewRuntime( &h100000 )
cx = JS_NewContext( rt, &h1000 )
global = JS_NewObject( cx, @global_class, NULL, NULL )
JS_InitStandardClasses( cx, global )
JS_SetErrorReporter(cx, @printError)
   
const JS_SCRIPT = "alert('hello');somenonexistentfunc()"

dim as jsval rval
Dim as JSFunction ptr _alert= JS_DefineFunction(cx, global,StrPtr("alert"), @alert, 1, 0)
if( JS_EvaluateScript( cx, global, JS_SCRIPT, len( JS_SCRIPT ), "localhost", 1, @rval) = 0 ) then
   MessageBox(0,*JS_GetStringBytes( JS_ValueToString( cx, rval ) ),"",MB_OK)
end if

JS_DestroyContext( cx )
JS_DestroyRuntime( rt )
deallocate( cx )
deallocate( rt )
deallocate( global )


Debuged with OllyDbg and the error is here:
Acess violation when reading


*Edit
This must be some problem in FB or it may be done in other way, this code give error too
JS_SetErrorReporter(cx, @printError)
Mabe we it dont parse the @function in a good way
I hope some can help :)
v1ctor
Site Admin
Posts: 3799
Joined: May 27, 2005 8:08
Location: SP / Bra[s]il
Contact:

Postby v1ctor » May 08, 2006 20:30

The callback signature isn't correct, the compiler will show a warning in that case.

Just add this to test.bas in the spidermonkey.zip package:

Code: Select all

function alert_cb cdecl _
   ( _
      byval cx as JSContext ptr, _
      byval obj as JSObject ptr, _
      byval argc as uintN, _
      byval argv as jsval ptr, _
      byval rval as jsval ptr _
   ) as JSBool
   
   if( argc < 1 ) then
      return 0
   end if
   
   print "message: "; *JS_GetStringBytes( JS_ValueToString( cx, argv[0] ) )

   return 1

end function

'' main
const JS_SCRIPT = "alert('hello');somenonexistentfunc()"
   
    dim as sm ptr sm = sm_create( )
   
    if( sm = 0 ) then
       end 1
    end if
       
   dim as jsval rval
   
   JS_DefineFunction( sm->cx, sm->global, "alert", @alert_cb, 1, 0 )
       
    print "script result: "; sm_eval( sm, JS_SCRIPT )
       
    sm_destroy( sm )
porfirio
Posts: 154
Joined: Mar 17, 2006 11:54
Location: Portugal

Postby porfirio » May 09, 2006 10:17

Oh god!

I was realy making it wrong :p

You rocks!

Thanks

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 1 guest