VirtualMachine* VirtualMachine::addArgument(const QString& arg) { QByteArray argBytes = arg.toUtf8(); int rc = unqlite_vm_config(m_pVm, UNQLITE_VM_CONFIG_ARGV_ENTRY, argBytes.data()); updateLastError(rc); return this; }
VirtualMachine* VirtualMachine::setVariable(const QString& name, const QVariant& value) { unqlite_value* pVal = createUnqlite(value); if (pVal) { QByteArray nameBytes = name.toUtf8(); int rc = unqlite_vm_config(m_pVm, UNQLITE_VM_CONFIG_CREATE_VAR, nameBytes.data(), pVal); unqlite_value_release(pVal); updateLastError(rc); } return this; }
char * extract_vm_output(unqlite_vm *pvm,int *length) { const void *buffer; char *t; //Extract the VM output unqlite_vm_config(pvm,UNQLITE_VM_CONFIG_EXTRACT_OUTPUT,&buffer,length); t=(char *)malloc(*length+1); memcpy(t,(const char*)buffer,*length); return t; }
/* No need for command line arguments, everything is stored in-memory */ int main(void) { unqlite *pDb; /* Database handle */ unqlite_vm *pVm; /* UnQLite VM resulting from successful compilation of the target Jx9 script */ int rc; puts(zBanner); /* Open our database */ rc = unqlite_open(&pDb,":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ){ Fatal(0,"Out of memory"); } /* Compile our Jx9 script defined above */ rc = unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm); if( rc != UNQLITE_OK ){ /* Compile error, extract the compiler error log */ const char *zBuf; int iLen; /* Extract error log */ unqlite_config(pDb,UNQLITE_CONFIG_JX9_ERR_LOG,&zBuf,&iLen); if( iLen > 0 ){ puts(zBuf); } Fatal(0,"Jx9 compile error"); } /* Install a VM output consumer callback */ rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,VmOutputConsumer,0); if( rc != UNQLITE_OK ){ Fatal(pDb,0); } /* Execute our script */ rc = unqlite_vm_exec(pVm); if( rc != UNQLITE_OK ){ Fatal(pDb,0); } /* Release our VM */ unqlite_vm_release(pVm); /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; }
Producer::Config::Config( const string& file, uint32_t id ) { // set default values prefix = Name("unnamed"); sigverif_delay = NanoSeconds( 30345 ); bloom_delay = NanoSeconds( 2535 ); // database and vm structs unqlite* db; unqlite_vm* vm; // initialize database int rc = unqlite_open( &db, ":mem:", UNQLITE_OPEN_READONLY ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: creating unqlite database: " << err << endl; exit(1); } // initialize unqlite vm rc = unqlite_compile_file( db, file.c_str(), &vm ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: compiling config script: " << err << endl; exit(1); } unqlite_value* id_val = unqlite_vm_new_scalar( vm ); unqlite_value_int64( id_val, id ); rc = unqlite_vm_config( vm, UNQLITE_VM_CONFIG_CREATE_VAR, "ID", id_val ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: exporting ID to config: " << err << endl; exit(1); } unqlite_vm_release_value( vm, id_val ); // execute config script rc = unqlite_vm_exec( vm ); if( rc != UNQLITE_OK ) { // something went wrong const char* err; int errlen; unqlite_config( db, UNQLITE_CONFIG_JX9_ERR_LOG, &err, &errlen ); cout << "Error: executing config script: " << err << endl; exit(1); } // retrieve config values const char* str; int len; unqlite_value* val; val = unqlite_vm_extract_variable( vm, "prefix" ); if( val ) { str = unqlite_value_to_string( val, &len ); prefix = string(str, len ); } val = unqlite_vm_extract_variable( vm, "sigverif_delay" ); if( unqlite_value_is_float( val ) ) sigverif_delay = Seconds( unqlite_value_to_double( val ) ); if( unqlite_value_is_int( val ) ) sigverif_delay = Seconds( unqlite_value_to_int64( val ) ); val = unqlite_vm_extract_variable( vm, "bloom_delay" ); if( unqlite_value_is_float( val ) ) bloom_delay = Seconds( unqlite_value_to_double( val ) ); if( unqlite_value_is_int( val ) ) bloom_delay = Seconds( unqlite_value_to_int64( val ) ); val = unqlite_vm_extract_variable( vm, "contents" ); if( val && unqlite_value_is_json_array( val ) ) { size_t count = unqlite_array_count( val ); for( size_t i = 0 ; i < count ; i++ ) { stringstream ss; string key; ss << i; ss >> key; unqlite_value* elem = unqlite_array_fetch ( val, key.c_str(), key.size() ); if( elem && unqlite_value_is_json_object( elem ) ) { unqlite_value* name_val = unqlite_array_fetch ( elem, "name", -1 ); unqlite_value* size_val = unqlite_array_fetch ( elem, "size", -1 ); unqlite_value* access_val = unqlite_array_fetch( elem, "access_level", -1 ); if( name_val && size_val && access_val && unqlite_value_is_string( name_val ) && unqlite_value_is_int( size_val ) && unqlite_value_is_int( access_val ) ) { str = unqlite_value_to_string( name_val, &len ); size_t size = unqlite_value_to_int( size_val ); uint8_t access_level = unqlite_value_to_int ( access_val ); contents.emplace( string( str, len ), Config::Content { size, access_level } ); } } } }
/* No need for command line arguments, everything is stored in-memory */ int main(void) { unqlite *pDb; /* Database handle */ unqlite_vm *pVm; /* UnQLite VM resulting from successful compilation of the target Jx9 script */ int rc; puts(zBanner); /* Open our database */ rc = unqlite_open(&pDb,":mem:" /* In-mem DB */,UNQLITE_OPEN_CREATE); if( rc != UNQLITE_OK ) { Fatal(0,"Out of memory"); } /* Compile our Jx9 script defined above */ rc = unqlite_compile(pDb,JX9_PROG,sizeof(JX9_PROG)-1,&pVm); if( rc != UNQLITE_OK ) { /* Compile error, extract the compiler error log */ const char *zBuf; int iLen; /* Extract error log */ unqlite_config(pDb,UNQLITE_CONFIG_JX9_ERR_LOG,&zBuf,&iLen); if( iLen > 0 ) { puts(zBuf); } Fatal(0,"Jx9 compile error"); } /* Now we have our program compiled, it's time to register our constants * and their associated C procedure. */ rc = unqlite_create_constant(pVm, "__PI__", PI_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __PI__ constant"); } rc = unqlite_create_constant(pVm, "__TIME__", TIME_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __TIME__ constant"); } rc = unqlite_create_constant(pVm, "__OS__", OS_Constant, 0); if( rc != UNQLITE_OK ) { Fatal(0,"Error while installing the __OS__ constant"); } /* Install a VM output consumer callback */ rc = unqlite_vm_config(pVm,UNQLITE_VM_CONFIG_OUTPUT,VmOutputConsumer,0); if( rc != UNQLITE_OK ) { Fatal(pDb,0); } /* Execute our script */ rc = unqlite_vm_exec(pVm); if( rc != UNQLITE_OK ) { Fatal(pDb,0); } /* Release our VM */ unqlite_vm_release(pVm); /* Auto-commit the transaction and close our database */ unqlite_close(pDb); return 0; }