questions on making jsextensions

testcase's icon

simplejs is a nice example of how to add an 'external object' to js but it only deals with basic atom types. is it possible to have an attr which is a JSObject of some other type - a closure for instance? maybe using an attr of _jit_sym_pointer or _jit_sym_object? would a setter function for this attr get an atom with a void pointer which could be cast to a JSObject in the JSAPI? Could try this out myself but just wanted to know if this was even possible.

Timothy Place's icon

If you look at the database support in JS we do something like this. You pass a SQLResult instance as an argument to the SQLite "exec" method. Then the SQLite "exec" method fills in the result.

The implementation of that exec method looks like this:

JSBool jssqlite_exec(JSContext *cx, uintN argc, jsval *vp)
{
    JSObject *obj=JS_THIS_OBJECT(cx,vp);
    jsval *argv=JS_ARGV(cx,vp);
    t_jssqlite *x = (t_jssqlite *)JS_GetPrivate(cx, obj);
    JSString     *str;
    JSObject    *jsob;
    t_jssqlresult *jssqlresult = NULL;
    t_symbol    *s;

    if(argc != 2){
        error("js: sqlite exec() requires 2 arguments (symbol, SQLResult) to define the query");
        return JS_FALSE;
    }
    str = JSVAL_TO_STRING(argv[0]);
    jsob = JSVAL_TO_OBJECT(argv[1]);
    
    jssqlresult = (t_jssqlresult *) JS_GetPrivate(cx, jsob);
    if(OB_INVALID(jssqlresult))
        return JS_FALSE;
    
    // could be more extensive in verifying the arg is a jssqlresult object
    
    s = jsstring_to_symbol(cx,str);
    if(s)
        object_method(x->sqlite_object, ps_exec, s, &jssqlresult->sqlite_result);
    else{
        error("js: sqlite exec() failed to create usable string for the query");
        return JS_FALSE;
    }
    return JS_TRUE;
}

testcase's icon

Thanks Timothy,
One thing that is not clear to me from the simplejs example - that object works by registering an A_GIMMEBACK method which gets passed a t_atom array for arguments and return value(s). The example you have here is what I was hoping was possible from my experience using JSAPI, but I don't see how it is supported in the public SDK. How does one add a method with this signature to the t_class?