- assigned_to: nobody --> tdz
I love this jsapigen, it hides the ugly bindingcode, resulting in cleaner code.
However, my dream would be to do inline javascriptcode.
Lots of things (stringmanipulation/concatenation/json) are VERY labourous/impossible in C.
Imagine how clean your code would be with code like this:
%{
#include <stdio.h>
static void
hello_world(void)
{
printf("Hello world!\n");
}
%}
function void js_hello_world : hello_world <static>;
function char* js_getFile : getFile <static>
%<
int main(){
char *foo = "some/file/in/a/dir/foo.txt";
JS_CompileFile(cx, global, "model.js");
// the following would do a JS_EvaluateScript of the jscode above, and parse the results
char *file = jsapi_charptr( "getFile( '%s' )", foo );
int weight = jsapi_int( "getObjectField('foo','weight')" );
}
>%
var count = 0;
var model{
objects: [{name:'foo', weight:1},{name:'flop', weight:2}]
}
function getObjectField( name, field ){
for( i in model.objects ) if( model.objects[i].name == name ) return model.objects[i][ field ];
}
function getFile(file){
count++;
if( !file.match("/") ) return file;
var parts = split("/", file);
return parts[ parts.length-1 ];
}
hello_world();
Tools like 'bin2h' or 'jsmin.c' could turn the javascript-snippet into a char-array (which can be executed by spidermonkeys JS_EvaluateScript() ).
Just an idea, because currently you need to load a whole js-file, even if you want to execute a simple javascript-function.
It would be really great to use javascript's json for datastructures,objects & arrays, and do lowlevel stuff with C.
It would make C Object Oriented without c++ :)
so basically shortcut/wrapper functions like:
jsapi_int( char* js_string, ... );
jsapi_charptr( char* js_string, ... );
jsapi_bool( char* js_string, ... );
jsapi_float( char* js_string, ... );
jsapi_double( char* js_string, ... );
..etc
with va_start optional parameters could be fetched to eventually do a sprintf() on js_string.
Will try to come up with some examples soon.