Exemplo n.º 1
0
template<> void ScriptInterface::ToJSVal<bool>(JSContext* UNUSED(cx), JS::MutableHandleValue ret, const bool& val)
{
	ret.setBoolean(val);
}
Exemplo n.º 2
0
bool DoStep(JSContext *cx, JS::HandleObject obj, JS::MutableHandleValue rval) {

	sqlite3_stmt *pStmt = (sqlite3_stmt*)JL_GetPrivate(obj);
	JL_ASSERT_THIS_OBJECT_STATE( pStmt );

	DatabasePrivate *dbpv;
	{

	JS::RootedValue dbVal(cx);
	JL_CHK( JL_GetReservedSlot(obj, SLOT_RESULT_DATABASE, &dbVal) );
	JL_ASSERT( dbVal.isObject() );


	JS::RootedObject dbValObj(cx, &dbVal.toObject());

	dbpv = (DatabasePrivate*)JL_GetPrivate(dbValObj);
	JL_ASSERT_OBJECT_STATE(dbpv, JL_GetClassName(dbValObj));

	}

	sqlite3 *db;
	db = dbpv->db;
	ASSERT( db == sqlite3_db_handle(pStmt) );

	{

	// check if bindings are up to date
	JS::RootedValue bindingUpToDate(cx);
	JL_CHK( JL_GetReservedSlot(obj, SLOT_RESULT_BINDING_UP_TO_DATE, &bindingUpToDate) );

	if ( bindingUpToDate != JSVAL_TRUE ) {

		JS::RootedValue queryArgument(cx);
		JS::RootedObject queryArgumentObj(cx);
		JL_CHK(jl::getSlot(cx, obj, SLOT_RESULT_QUERY_ARGUMENT_OBJECT, &queryArgumentObj));



		JL_CHK(SqliteSetupBindings(cx, pStmt, queryArgumentObj, obj)); // ":" use result object. "@" is the object passed to Query()
		JL_CHK( JL_SetReservedSlot(obj, SLOT_RESULT_BINDING_UP_TO_DATE, JL_TRUE) );
		// doc: The sqlite3_bind_*() routines must be called AFTER sqlite3_prepare() or sqlite3_reset() and BEFORE sqlite3_step().
		//      Bindings are not cleared by the sqlite3_reset() routine. Unbound parameters are interpreted as NULL.
	}

	}

	dbpv->tmpcx = cx;
	int status;
	status = sqlite3_step( pStmt ); // The return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE.
	dbpv->tmpcx = NULL;
	
	JL_CHK( !JL_IsExceptionPending(cx) );

	switch ( status ) {

		case SQLITE_ROW: // SQLITE_ROW is returned each time a new row of data is ready for processing by the caller
			rval.setBoolean(true);
			return true;
		case SQLITE_DONE: // means that the statement has finished executing successfully. sqlite3_step() should not be called again on this virtual machine without first calling sqlite3_reset() to reset the virtual machine back to its initial state.
			rval.setBoolean(false);
			return true;
		case SQLITE_MISUSE:
			// doc. means that the this routine was called inappropriately. Perhaps it was called on a virtual machine that had already been finalized or on one that had previously returned SQLITE_ERROR or SQLITE_DONE.
			//      Or it could be the case that a database connection is being used by a different thread than the one it was created it.
			// doc. If an interface fails with SQLITE_MISUSE, that means the interface was invoked incorrectly by the application. In that case, the error code and message may or may not be set.
			JL_CHK( SqliteThrowError(cx, db) );
//		case SQLITE_ERROR:
//		case SQLITE_SCHEMA: // (TBD) check for another error (doc. The database schema changed)
//			JL_CHK( SqliteThrowError(cx, db) );
	}
//	JL_REPORT_ERROR("invalid case (status:%d)", status );

	JL_CHK( SqliteThrowError(cx, db) );
	JL_BAD;
}
static void
convertValue(JSContext *             jct,
             JS::MutableHandleValue  theData,
             const yarp::os::Value & inputValue)
{
    ODL_ENTER(); //####
    ODL_P2("jct = ", jct, "inputValue = ", &inputValue); //####
    if (inputValue.isBool())
    {
        theData.setBoolean(inputValue.asBool());
    }
    else if (inputValue.isInt())
    {
        theData.setInt32(inputValue.asInt());
    }
    else if (inputValue.isString())
    {
        YarpString value = inputValue.asString();
        JSString * aString = JS_NewStringCopyZ(jct, value.c_str());

        if (aString)
        {
            theData.setString(aString);
        }
    }
    else if (inputValue.isDouble())
    {
        theData.setDouble(inputValue.asDouble());
    }
    else if (inputValue.isDict())
    {
        yarp::os::Property * value = inputValue.asDict();

        if (value)
        {
            yarp::os::Bottle asList(value->toString());

            convertDictionary(jct, theData, asList);
        }
    }
    else if (inputValue.isList())
    {
        yarp::os::Bottle * value = inputValue.asList();

        if (value)
        {
            yarp::os::Property asDict;

            if (ListIsReallyDictionary(*value, asDict))
            {
                convertDictionary(jct, theData, *value);
            }
            else
            {
                convertList(jct, theData, *value);
            }
        }
    }
    else
    {
        // We don't know what to do with this...
        theData.setNull();
    }
    ODL_EXIT(); //####
} // convertValue