Reply To: questions on making jsextensions


Forums > Dev > questions on making jsextensions
August 21, 2013 | 9:41 am

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;
}