void StyledStreamWriter::writeValue( const Value &value ) { switch ( value.type() ) { case nullValue: pushValue( "null" ); break; case intValue: pushValue( valueToString( value.asInt() ) ); break; case uintValue: pushValue( valueToString( value.asUInt() ) ); break; case realValue: pushValue( valueToString( value.asDouble() ) ); break; case stringValue: pushValue( valueToQuotedString( value.asCString() ) ); break; case booleanValue: pushValue( valueToString( value.asBool() ) ); break; case arrayValue: writeArrayValue( value); break; case objectValue: { Value::Members members( value.getMemberNames() ); if ( members.empty() ) pushValue( "{}" ); else { writeWithIndent( "{" ); indent(); Value::Members::iterator it = members.begin(); while ( true ) { const std::string &name = *it; const Value &childValue = value[name]; writeCommentBeforeValue( childValue ); writeWithIndent( valueToQuotedString( name.c_str() ) ); *document_ << " : "; writeValue( childValue ); if ( ++it == members.end() ) { writeCommentAfterValueOnSameLine( childValue ); break; } *document_ << ","; writeCommentAfterValueOnSameLine( childValue ); } unindent(); writeWithIndent( "}" ); } } break; } }
void StyledStreamWriter::writeArrayValue ( const Value& value ) { unsigned size = value.size (); if ( size == 0 ) pushValue ( "[]" ); else { bool isArrayMultiLine = isMultineArray ( value ); if ( isArrayMultiLine ) { writeWithIndent ( "[" ); indent (); bool hasChildValue = !childValues_.empty (); unsigned index = 0; while ( true ) { const Value& childValue = value[index]; if ( hasChildValue ) writeWithIndent ( childValues_[index] ); else { writeIndent (); writeValue ( childValue ); } if ( ++index == size ) break; *document_ << ","; } unindent (); writeWithIndent ( "]" ); } else // output on a single line { assert ( childValues_.size () == size ); *document_ << "[ "; for ( unsigned index = 0; index < size; ++index ) { if ( index > 0 ) *document_ << ", "; *document_ << childValues_[index]; } *document_ << " ]"; } } }
void draw() { background(0); drawGrid(); val = getValue(); if (val != -1) { pushValue(val); } drawLines(); }
bool VariableRef::readUInt32( uint32& out ) const { VALIDATE(); pushValue(); out = (uint32)lua_tonumber(m_ownerContext->getPimpl()->L, -1); popValue(); return true; }
bool VariableRef::readFloat( float& out ) const { VALIDATE(); pushValue(); out = (float)lua_tonumber(m_ownerContext->getPimpl()->L, -1); popValue(); return true; }
//// Listener settings //// void setupListener() { auto listener_node = *m_dev->emplace(m_dev->children().cend(), "listener"); auto listener_pos_node = *listener_node->emplace(listener_node->children().cend(), "pos"); add_position(listener_pos_node, make_parameter( [&] () { return m_scene.listener().Position(); }, [&] (const auto& elt) { m_scene.listener().Position(elt); })); auto listener_orient_node = *listener_node->emplace(listener_node->children().cend(), "orientation"); auto listener_orient_addr = listener_orient_node->createAddress(OSSIA::Value::Type::TUPLE); // [ at_x at_y at_z up_x at_y at_z ] auto tupl = new OSSIA::Tuple; const auto& orient = m_scene.listener().Orientation(); tupl->value.push_back(new OSSIA::Float(orient.At()[0])); tupl->value.push_back(new OSSIA::Float(orient.At()[1])); tupl->value.push_back(new OSSIA::Float(orient.At()[2])); tupl->value.push_back(new OSSIA::Float(orient.Up()[0])); tupl->value.push_back(new OSSIA::Float(orient.Up()[1])); tupl->value.push_back(new OSSIA::Float(orient.Up()[2])); listener_orient_addr->pushValue(tupl); listener_orient_addr->addCallback([&] (const OSSIA::Value* val) { auto tpl = dynamic_cast<const OSSIA::Tuple*>(val); if(tpl->value.size() != 6) return; auto at_x = dynamic_cast<const OSSIA::Float*>(tpl->value[0]); auto at_y = dynamic_cast<const OSSIA::Float*>(tpl->value[1]); auto at_z = dynamic_cast<const OSSIA::Float*>(tpl->value[2]); auto up_x = dynamic_cast<const OSSIA::Float*>(tpl->value[3]); auto up_y = dynamic_cast<const OSSIA::Float*>(tpl->value[4]); auto up_z = dynamic_cast<const OSSIA::Float*>(tpl->value[5]); if(!at_x || !at_y || !at_z || !up_x || !up_y || !up_z) return; m_scene.listener().Orientation(at_x->value, at_y->value, at_z->value, up_x->value, up_y->value, up_z->value); }); auto listener_orient_at_node = *listener_orient_node->emplace(listener_orient_node->children().cend(), "at"); add_position(listener_orient_at_node, make_parameter( [&] () { return m_scene.listener().OrientationAt(); }, [&] (const auto& elt) { m_scene.listener().Orientation(elt, m_scene.listener().OrientationUp()); } )); auto listener_orient_up_node = *listener_orient_node->emplace(listener_orient_node->children().cend(), "up"); add_position(listener_orient_up_node, make_parameter( [&] () { return m_scene.listener().OrientationUp(); }, [&] (const auto& elt) { m_scene.listener().Orientation(m_scene.listener().OrientationAt(), elt); } )); }
int luaopen_input(lua_State* L) { luaL_register(L, "input", inputlib_f); // event table lua_pushstring(L, "event"); lua_newtable(L); lua_pushstring(L, "key"); pushValue(L, W_INPUT.KeyEvent); lua_settable(L, -3); lua_pushstring(L, "mouseButton"); pushValue(L, W_INPUT.MouseButtonEvent); lua_settable(L, -3); lua_pushstring(L, "cursorPos"); pushValue(L, W_INPUT.CursorPositionEvent); lua_settable(L, -3); lua_settable(L, -3); // end event table lua_pushstring(L, "action"); push_action_constants(L); lua_settable(L, -3); lua_pushstring(L, "key"); push_keyboard_constants(L); lua_settable(L, -3); lua_pushstring(L, "mouse"); push_mouse_constants(L); lua_settable(L, -3); lua_pushstring(L, "cursorMode"); push_cursor_mode_constants(L); lua_settable(L, -3); return 1; }
int pushValue(lua_State* state, const std::vector<T>& vec) { lua_createtable(state, vec.size(), 0); for(std::size_t i = 0; i < vec.size(); i++) { pushValue(state, vec[i]); lua_rawseti(state, -2, i + 1); } return 1; }
//------------------------------------------------------------------------------ void SN_API pushArray(HSQUIRRELVM vm, const Variant::Array & a) { sq_newarray(vm, a.size()); for (size_t i = 0; i < a.size(); ++i) { sq_pushinteger(vm, i); pushValue(vm, a[i]); if (SQ_FAILED(sq_set(vm, -3))) { SN_WARNING("Variant => Squirrel: failed to set array index " << i); } } }
void StyledWriter::writeArrayValue( const Value &value ) { unsigned size = value.size(); if ( size == 0 ) pushValue( "[]" ); else { bool isArrayMultiLine = isMultineArray( value ); if ( isArrayMultiLine ) { writeWithIndent( "[" ); indent(); bool hasChildValue = !childValues_.empty(); unsigned index =0; for (;;) { const Value &childValue = value[index]; writeCommentBeforeValue( childValue ); if ( hasChildValue ) writeWithIndent( childValues_[index] ); else { writeIndent(); writeValue( childValue ); } if ( ++index == size ) { writeCommentAfterValueOnSameLine( childValue ); break; } document_ += ","; writeCommentAfterValueOnSameLine( childValue ); } unindent(); writeWithIndent( "]" ); } else // output on a single line { assert( childValues_.size() == size ); document_ += "[ "; for ( unsigned index =0; index < size; ++index ) { if ( index > 0 ) document_ += ", "; document_ += childValues_[index]; } document_ += " ]"; } } }
static void s_implementation(gnative_Object *obj, gnative_Function *function, gnative_Value *parameters) { if (L == NULL) return; gnative_Class *cls = gnative_ObjectGetClass(obj); lua_pushlightuserdata(L, &keyClasses); lua_gettable(L, LUA_REGISTRYINDEX); lua_pushlightuserdata(L, cls); lua_gettable(L, -2); lua_remove(L, -2); lua_pushstring(L, "__functions"); lua_rawget(L, -2); lua_pushlightuserdata(L, function); lua_gettable(L, -2); gnative_Value v; v.obj = obj; pushValue(L, v, GNATIVE_TYPE_OBJECT, false); int parameterCount = gnative_FunctionGetParameterCount(function); gnative_Type *parameterTypes = gnative_FunctionGetParameterTypes(function); for (int i = 0; i < parameterCount; ++i) pushValue(L, parameters[i], parameterTypes[i], false); lua_call(L, 1 + parameterCount, 0); lua_pop(L, 2); }
void BuiltStyledStreamWriter::writeValue(Value const& value) { switch (value.type()) { case nullValue: pushValue(nullSymbol_); break; case intValue: pushValue(valueToString(value.asLargestInt())); break; case uintValue: pushValue(valueToString(value.asLargestUInt())); break; case realValue: pushValue(valueToString(value.asDouble())); break; case stringValue: pushValue(valueToQuotedString(value.asCString())); break; case booleanValue: pushValue(valueToString(value.asBool())); break; case arrayValue: writeArrayValue(value); break; case objectValue: { Value::Members members(value.getMemberNames()); if (members.empty()) pushValue("{}"); else { writeWithIndent("{"); indent(); Value::Members::iterator it = members.begin(); for (;;) { std::string const& name = *it; Value const& childValue = value[name]; writeCommentBeforeValue(childValue); writeWithIndent(valueToQuotedString(name.c_str())); sout_ << colonSymbol_; writeValue(childValue); if (++it == members.end()) { writeCommentAfterValueOnSameLine(childValue); break; } sout_ << ","; writeCommentAfterValueOnSameLine(childValue); } unindent(); writeWithIndent("}"); } } break; } }
void LuaModule::pushJsonScalarValue(const Json::Value &key, const Json::Value &val, lua_State * stack) { if (val.isObject() || val.isArray()) { throw std::runtime_error("Not a scalar value"); } if (key.isString()) { pushKey(key, stack); } pushValue(val, stack); if (key.isNumeric()) { lua_rawseti(stack, -2, key.asInt() + 1); } else if (key.isString()) { lua_settable(stack, -3); } }
bool VariableRef::readBytes( size_t num, uint8* out_buf ) const { VALIDATE(); lua_State* L = m_ownerContext->getPimpl()->L; bool success = false; pushValue(); if (lua_istable(L, -1) == 1) { success = true; lua_pushnil(L); for (size_t i = 0; i < num; ++i) { const int next_ret = lua_next(L, -2); if (next_ret == 0) { log_error("Not enough values in table (%d required, has %d)", num, i); success = false; break; } if (lua_isnumber(L, -1) == 0) { log_error("Non-number value in table when reading bytes"); success = false; break; } // Checks for fractional parts, too high values etc... const uint8 val = (uint8)lua_tonumber(L, -1); out_buf[i] = val; lua_pop(L, 1); } lua_pop(L, 1); } popValue(); return true; }
bool VariableRef::readString( mkString& out ) const { VALIDATE(); bool success = false; pushValue(); const char* ptr = lua_tolstring(m_ownerContext->getPimpl()->L, -1, NULL); if (ptr) { success = true; out = ptr; } popValue(); return success; }
void BuiltStyledStreamWriter::writeArrayValue(Value const& value) { unsigned size = value.size(); if (size == 0) pushValue("[]"); else { bool isMultiLine = (cs_ == CommentStyle::All) || isMultineArray(value); if (isMultiLine) { writeWithIndent("["); indent(); bool hasChildValue = !childValues_.empty(); unsigned index = 0; for (;;) { Value const& childValue = value[index]; writeCommentBeforeValue(childValue); if (hasChildValue) writeWithIndent(childValues_[index]); else { if (!indented_) writeIndent(); indented_ = true; writeValue(childValue); indented_ = false; } if (++index == size) { writeCommentAfterValueOnSameLine(childValue); break; } sout_ << ","; writeCommentAfterValueOnSameLine(childValue); } unindent(); writeWithIndent("]"); } else // output on a single line { assert(childValues_.size() == size); sout_ << "["; if (!indentation_.empty()) sout_ << " "; for (unsigned index = 0; index < size; ++index) { if (index > 0) sout_ << ", "; sout_ << childValues_[index]; } if (!indentation_.empty()) sout_ << " "; sout_ << "]"; } } }
void Lua::callFunction(const char* name, const std::vector<Data>& args, std::vector<Data>* result) { pushGlobalVariable(name); for (const auto& arg: args) { pushValue(arg); } std::size_t numReturn = result ? result->size() : 0; handleError(lua_pcall(handle, args.size(), numReturn, 0)); if (result) { std::size_t stackSize = lua_gettop(handle); assert(stackSize >= numReturn); for (std::size_t i = 0; i < numReturn; ++i) { (*result)[i] = getValue(stackSize - numReturn + i + 1); } pop(numReturn); } }
//------------------------------------------------------------------------------ void SN_API pushTable(HSQUIRRELVM vm, const Variant::Dictionary & d) { sq_newtable(vm); for (auto it = d.begin(); it != d.end(); ++it) { // Push key const std::string & key = it->first; sq_pushstring(vm, key.c_str(), key.size()); // Push value const Variant & value = it->second; pushValue(vm, value); // Set or create if (SQ_FAILED(sq_newslot(vm, -3, SQFalse))) { SN_WARNING("Variant => Squirrel: failed to set key " << key); } } }
void LuaInterface::clearTable(int index) { assert(hasIndex(index) && isTable(index)); pushNil(); // table, nil bool stop = false; while(next(index-1)) { // table, key, value pop(); // table, key pushValue(); // table, key, key if(next(index-2)) { // table, key, nextkey, value pop(); // table, key, nextkey insert(-2); // table, nextkey, key pushNil(); // table, nextkey, key, nil rawSet(index-3); // table, nextkey } else { // table, key pushNil(); // table, key, nil rawSet(index-2); // table break; } } }
void Feature::updateWithValue(double value, bool computeStats, int numFrames) { if (std::isnan(value) || std::isinf(value)) value = 0.0; pushValue(value); if (computeStats) { m_accLtf(value); m_accStf = doubleAcc(); for (int idx = 0; idx < std::min<int>(numFrames, m_history.size()); idx++) { m_accStf(m_history[idx]); } m_means.push_back(acc::mean(m_accStf)); m_stds.push_back(sqrt(acc::variance(m_accStf))); } }
void add_position(std::shared_ptr<OSSIA::Node> node, Parameter_t&& param) { static auto modify_i = [=] (int i) { return [=] (const OSSIA::Value* val) { auto float_val = dynamic_cast<const OSSIA::Float*>(val); if(!float_val) return; auto elt = param.get(); elt[i] = float_val->value; param.set(elt); }; }; auto addr = node->createAddress(OSSIA::Value::Type::TUPLE); // [ x y z ] auto tupl = new OSSIA::Tuple; tupl->value.push_back(new OSSIA::Float(param.get()[0])); tupl->value.push_back(new OSSIA::Float(param.get()[1])); tupl->value.push_back(new OSSIA::Float(param.get()[2])); addr->pushValue(tupl); addr->addCallback([=] (const OSSIA::Value* val) { auto tpl = dynamic_cast<const OSSIA::Tuple*>(val); if(tpl->value.size() != 3) return; auto x = dynamic_cast<const OSSIA::Float*>(tpl->value[0]); auto y = dynamic_cast<const OSSIA::Float*>(tpl->value[1]); auto z = dynamic_cast<const OSSIA::Float*>(tpl->value[2]); if(!x || !y || !z) return; param.set(oalplus::Vec3f{x->value, y->value, z->value}); }); add_float_child(node, "x", modify_i(0)); add_float_child(node, "y", modify_i(1)); add_float_child(node, "z", modify_i(2)); }
//------------------------------------------------------------------------------ void applyProperties(HSQUIRRELVM vm, const Variant::Dictionary & o, SQInteger objectIndex) { if (objectIndex < 0) objectIndex -= 2; // Key + value stack offset for (auto it = o.begin(); it != o.end(); ++it) { // Push key const std::string & key = it->first; sq_pushstring(vm, key.c_str(), key.size()); // Push value // TODO If the value is a table, go recursively instead of overwriting the previous value const Variant & value = it->second; pushValue(vm, value); // Set if (SQ_FAILED(sq_set(vm, objectIndex))) { SN_WARNING("Variant => Squirrel: failed to set key " << key); } } }
void EntityTemplateInterpreter::readInt64(std::vector<char>& code, int& i) { int64_t *val = reinterpret_cast<int64_t*>(&code[i]); i += sizeof(int64_t); pushValue(val); }
void BytecodeInterpreter::interpret() { while (bci() < bc()->length()) { bool jmp = false; Value first; Value second; switch (bc()->getInsn(bci())) { case BC_STOP: return; case BC_DLOAD0: pushValue(0.0); break; case BC_ILOAD0: pushValue<int64_t>(0); break; case BC_DLOAD1: pushValue(1.0); break; case BC_ILOAD1: pushValue<int64_t>(1); break; case BC_DLOADM1: pushValue(-1.0); break; case BC_ILOADM1: pushValue<int64_t>(-1); break; case BC_DLOAD: pushValue(bc()->getDouble(bci() + 1)); break; case BC_ILOAD: pushValue(bc()->getInt64(bci() + 1)); break; case BC_SLOAD: pushValue(m_code->constantById(bc()->getUInt16(bci() + 1)).c_str()); break; case BC_CALLNATIVE: pushValue(callNativeFunction(bc()->getUInt16(bci() + 1))); break; case BC_CALL: m_locals.push(bc()->getUInt16(bci() + 1)); pushFunc(bc()->getUInt16(bci() + 1)); pushBci(); jmp = true; break; case BC_RETURN: m_locals.pop(); popFunc(); popBci(); //jmp = true; break; #define LOAD_VAR_N(type, n) \ pushValue(m_locals.load(1, n).type()); \ break; case BC_LOADDVAR0: LOAD_VAR_N(doubleValue, 0) case BC_LOADDVAR1: LOAD_VAR_N(doubleValue, 1) case BC_LOADDVAR2: LOAD_VAR_N(doubleValue, 2) case BC_LOADDVAR3: LOAD_VAR_N(doubleValue, 3) case BC_LOADIVAR0: LOAD_VAR_N(intValue, 0) case BC_LOADIVAR1: LOAD_VAR_N(intValue, 1) case BC_LOADIVAR2: LOAD_VAR_N(intValue, 2) case BC_LOADIVAR3: LOAD_VAR_N(intValue, 3) case BC_LOADSVAR0: LOAD_VAR_N(stringValue, 0) case BC_LOADSVAR1: LOAD_VAR_N(stringValue, 1) case BC_LOADSVAR2: LOAD_VAR_N(stringValue, 2) case BC_LOADSVAR3: LOAD_VAR_N(stringValue, 3) #undef LOAD_VAR_N #define STORE_VAR_N(type, n) \ pushValue(m_locals.load(1, n).type()); \ break; case BC_STOREDVAR0: STORE_VAR_N(doubleValue, 0) case BC_STOREDVAR1: STORE_VAR_N(doubleValue, 1) case BC_STOREDVAR2: STORE_VAR_N(doubleValue, 2) case BC_STOREDVAR3: STORE_VAR_N(doubleValue, 3) case BC_STOREIVAR0: STORE_VAR_N(intValue, 0) case BC_STOREIVAR1: STORE_VAR_N(intValue, 1) case BC_STOREIVAR2: STORE_VAR_N(intValue, 2) case BC_STOREIVAR3: STORE_VAR_N(intValue, 3) case BC_STORESVAR0: STORE_VAR_N(stringValue, 0) case BC_STORESVAR1: STORE_VAR_N(stringValue, 1) case BC_STORESVAR2: STORE_VAR_N(stringValue, 2) case BC_STORESVAR3: STORE_VAR_N(stringValue, 3) #undef STORE_VAR_N #define LOAD_VAR(type) \ pushValue(m_locals.load(1, bc()->getUInt16(bci() + 1)).type()); \ break; case BC_LOADDVAR: LOAD_VAR(doubleValue) case BC_LOADIVAR: LOAD_VAR(intValue) case BC_LOADSVAR: LOAD_VAR(stringValue) #undef LOAD_CTX_VAR #define STORE_VAR(type) \ m_locals.store(popValue().type(), 1, bc()->getUInt16(bci() + 1)); \ break; case BC_STOREDVAR: STORE_VAR(doubleValue) case BC_STOREIVAR: STORE_VAR(intValue) case BC_STORESVAR: STORE_VAR(stringValue) #undef STORE_VAR #define LOAD_CTX_VAR(type) \ pushValue(m_locals.load(bc()->getUInt16(bci() + 1), \ bc()->getUInt16(bci() + 3)).type()); \ break; case BC_LOADCTXDVAR: LOAD_CTX_VAR(doubleValue) case BC_LOADCTXIVAR: LOAD_CTX_VAR(intValue) case BC_LOADCTXSVAR: LOAD_CTX_VAR(stringValue) #undef LOAD_CTX_VAR #define STORE_CTX_VAR(type) \ m_locals.store(popValue().type(), \ bc()->getUInt16(bci() + 1), \ bc()->getUInt16(bci() + 3)); \ break; case BC_STORECTXDVAR: STORE_CTX_VAR(doubleValue) case BC_STORECTXIVAR: STORE_CTX_VAR(intValue) case BC_STORECTXSVAR: STORE_CTX_VAR(stringValue) #undef STORE_CTX_VAR #define CMP_JMP(op) \ first = popValue(); \ second = popValue(); \ if (first.intValue() op second.intValue()) { \ bci() += bc()->getInt16(bci() + 1) + 1; \ jmp = true; \ } \ break; case BC_IFICMPNE: CMP_JMP(!=) case BC_IFICMPE: CMP_JMP(==) case BC_IFICMPG: CMP_JMP(>) case BC_IFICMPGE: CMP_JMP(>=) case BC_IFICMPL: CMP_JMP(<) case BC_IFICMPLE: CMP_JMP(<=) #undef CMP_JMP case BC_JA: bci() += bc()->getInt16(bci() + 1) + 1; jmp = true; break; #define BINARY_OP(type, op) \ first = popValue(); \ second = popValue(); \ pushValue(first.type() op second.type()); \ break; case BC_DADD: BINARY_OP(doubleValue, +) case BC_IADD: BINARY_OP(intValue, +) case BC_DSUB: BINARY_OP(doubleValue, -) case BC_ISUB: BINARY_OP(intValue, -) case BC_DMUL: BINARY_OP(doubleValue, *) case BC_IMUL: BINARY_OP(intValue, *) case BC_DDIV: BINARY_OP(doubleValue, /) case BC_IDIV: BINARY_OP(intValue, /) case BC_IMOD: BINARY_OP(intValue, %) case BC_IAOR: BINARY_OP(intValue, |) case BC_IAAND: BINARY_OP(intValue, &) case BC_IAXOR: BINARY_OP(intValue, ^) #undef BINARY_OP #define CMP(type) \ first = popValue(); \ second = popValue(); \ if (first.type() < second.type()) \ pushValue<int64_t>(-1); \ else if (first.type() == second.type()) \ pushValue<int64_t>(0); \ else \ pushValue<int64_t>(1); \ break; case BC_DCMP: CMP(doubleValue) case BC_ICMP: CMP(intValue) #undef CMP case BC_DNEG: pushValue(-popValue().doubleValue()); break; case BC_INEG: pushValue(-popValue().intValue()); break; case BC_S2I: first = popValue(); pushValue<int64_t>(first.stringValue() != 0); break; case BC_I2D: pushValue<double>(popValue().intValue()); break; case BC_D2I: pushValue<int64_t>(popValue().doubleValue()); break; case BC_SWAP: first = popValue(); second = popValue(); pushValue(first); pushValue(second); break; case BC_POP: popValue(); break; case BC_IPRINT: writeValue(std::cout, popValue(), VT_INT); break; case BC_DPRINT: writeValue(std::cout, popValue(), VT_DOUBLE); break; case BC_SPRINT: writeValue(std::cout, popValue(), VT_STRING); break; case BC_DUMP: first = popValue(); writeValue(std::cerr, first, first.type()); break; case BC_BREAK: case BC_SLOAD0: default: throw BytecodeException("Unsupported bytecode"); } moveBci(jmp); } throw BytecodeException("STOP bytecode is not found"); }
//-------------------------------------------------------------------- // EXTERNAL FUNCTIONS //-------------------------------------------------------------------- int8_t execute(dvm_state_t* dvm_st, DvmState *eventState) { DVMBasiclib_state_t *s = (&dvm_st->basiclib_st); DvmContext *context = &(eventState->context); while ((context->state == DVM_STATE_RUN) && (context->num_executed < DVM_CPU_SLICE)) { DvmOpcode instr = getOpcode(dvm_st, context->which, context->pc); DEBUG("-----------------------------------------------\n"); DEBUG("[BASIC_LIB] execute: (%d) PC: %d. INSTR: %d\n",context->which, context->pc, instr); if(instr & LIB_ID_BIT) { //Extension Library opcode encountered return SOS_OK; } context->num_executed++; switch(instr) { case OP_START: { __asm __volatile("__sleep_measure_start:"); context->pc += 1; break; } case OP_STOP: { context->pc += 1; break; } case OP_HALT: { DEBUG("<<<<<<<<<<<=====================>>>>>>>>>>>\n"); DEBUG("[BASIC_LIB] execute: (%d): HALT executed.\n", (int)context->which); haltContext(dvm_st, context); context->state = DVM_STATE_HALT; context->pc = 0; break; } case OP_LED: { DvmStackVariable* arg = popOperand( eventState); led_op(arg->value.var); context->pc += 1; break; } case OP_GETVAR + 0: case OP_GETVAR + 1: case OP_GETVAR + 2: case OP_GETVAR + 3: case OP_GETVAR + 4: case OP_GETVAR + 5: case OP_GETVAR + 6: case OP_GETVAR + 7: { uint8_t arg = instr - OP_GETVAR; DEBUG("[BASIC_LIB] execute: OPGETVAR (%d):: Pushing value %d.\n", (int)arg,(int)s->shared_vars[arg].value.var); pushOperand( eventState, &s->shared_vars[arg]); context->pc += 1; break; } case OP_SETVAR + 0: case OP_SETVAR + 1: case OP_SETVAR + 2: case OP_SETVAR + 3: case OP_SETVAR + 4: case OP_SETVAR + 5: case OP_SETVAR + 6: case OP_SETVAR + 7: { uint8_t arg = instr - OP_SETVAR; DvmStackVariable* var = popOperand( eventState); DEBUG("[BASIC_LIB] execute: OPSETVAR (%d):: Setting value to %d.\n",(int)arg,(int)var->value.var); s->shared_vars[arg] = *var; context->pc += 1; break; } case OP_GETVARF + 0: case OP_GETVARF + 1: case OP_GETVARF + 2: case OP_GETVARF + 3: case OP_GETVARF + 4: case OP_GETVARF + 5: case OP_GETVARF + 6: case OP_GETVARF + 7: { // Use for type casting an integer shared var to float uint8_t arg = instr - OP_GETVARF; int32_t res = 0; uint16_t res_part = 0; DEBUG("[BASIC_LIB] execute: OPGETVARF (%d):: Pushing value %d.\n", (int)arg,(int)s->shared_vars[arg].value.var); res = (int32_t)(s->shared_vars[arg].value.var * FLOAT_PRECISION); res_part = res & 0xFFFF; pushValue( eventState, res_part, DVM_TYPE_FLOAT_DEC); res_part = res >> 16; pushValue( eventState, res_part, DVM_TYPE_FLOAT); context->pc += 1; break; } case OP_SETVARF + 0: case OP_SETVARF + 1: case OP_SETVARF + 2: case OP_SETVARF + 3: case OP_SETVARF + 4: case OP_SETVARF + 5: case OP_SETVARF + 6: { // Type-casting an integer to float and saving it in shared var uint8_t arg = instr - OP_SETVARF; DvmStackVariable* var = popOperand( eventState); int32_t res = 0; uint16_t res_part; DEBUG("[BASIC_LIB] execute: OPSETVARF (%d):: Setting value to %d.\n",(int)arg,(int)var->value.var); res = (int32_t)(var->value.var * FLOAT_PRECISION); res_part = res & 0xFFFF; s->shared_vars[arg+1].type = DVM_TYPE_FLOAT_DEC; s->shared_vars[arg+1].value.var = res_part; res_part = res >> 16; s->shared_vars[arg].type = DVM_TYPE_FLOAT; s->shared_vars[arg].value.var = res_part; context->pc += 1; break; } case OP_SETTIMER + 0: case OP_SETTIMER + 1: case OP_SETTIMER + 2: case OP_SETTIMER + 3: case OP_SETTIMER + 4: case OP_SETTIMER + 5: case OP_SETTIMER + 6: case OP_SETTIMER + 7: { uint32_t msec; uint8_t timerID = instr - OP_SETTIMER; DvmStackVariable* arg = popOperand( eventState); DEBUG("[BASIC_LIB] execute: Setting Timer %d period to %d.\n", timerID, arg->value.var); //msec = 102 * arg->value.var + (4 * arg->value.var) / 10; // Set the timer timeout argument in seconds msec = arg->value.var * 4;// * 1000; DEBUG("[BASIC_LIB] execute: <<<<< WARNING - Timer %d not being stopped >>>>\n", timerID); // sys_timer_stop(timerID); if (msec > 0) { // Ram - Where is the init ?? sys_timer_start(timerID, msec, TIMER_REPEAT); DEBUG("[BASIC_LIB] execute: Timer ID: %d started. Period: %d msec.\n", timerID, msec); } context->pc += 1; break; } case OP_RAND: { DvmStackVariable* arg = popOperand( eventState); uint16_t rnd; rnd = sys_rand() % arg->value.var; pushValue( eventState, rnd, DVM_TYPE_INTEGER); context->pc += 1; break; } /* case OP_JMP: case OP_JNZ: case OP_JZ: case OP_JG: case OP_JGE: case OP_JL: case OP_JLE: case OP_JE: case OP_JNE: case OP_ADD: case OP_SUB: case OP_DIV: case OP_MUL: case OP_ABS: case OP_MOD: case OP_INCR: case OP_DECR: { mathlib_executeDL(s->mathlib_execute, eventState, instr); break; } */ // Math Lib case OP_ADD: case OP_SUB: case OP_DIV: case OP_MUL: { DvmStackVariable* arg1 = popOperand( eventState); DvmStackVariable* arg2 = popOperand( eventState); DvmStackVariable* arg3 = NULL, *arg4 = NULL; int32_t fl_arg1, fl_arg2; int32_t res = 0; uint16_t res_part; int16_t int_res = 0; if (arg1->type == DVM_TYPE_FLOAT) { fl_arg1 = convert_to_float(arg1, arg2); arg3 = popOperand( eventState); if (arg3->type == DVM_TYPE_FLOAT) { // FLOAT <op> FLOAT arg4 = popOperand( eventState); fl_arg2 = convert_to_float(arg3, arg4); if(instr == OP_ADD) { res = (int32_t)(fl_arg1 + fl_arg2); DEBUG("[BASIC_LIB] execute: FLOAT ADD FLOAT %d\n", res); } else if(instr == OP_SUB) { res = (int32_t)(fl_arg1 - fl_arg2); DEBUG("[BASIC_LIB] execute: FLOAT SUB FLOAT %d\n", res); } else if(instr == OP_DIV) { res = (int32_t)((fl_arg1 * FLOAT_PRECISION) / fl_arg2); DEBUG("[BASIC_LIB] execute: FLOAT DIV FLOAT: %d\n", res); } else if(instr == OP_MUL) { res = (int32_t)((fl_arg1 * fl_arg2) / FLOAT_PRECISION); DEBUG("[BASIC_LIB] execute: FLOAT MULT FLOAT %d\n", res); } } else { // FLOAT <OP> INTEGER if(instr == OP_ADD) { res = (int32_t)(fl_arg1 + (arg3->value.var * FLOAT_PRECISION)); DEBUG("[BASIC_LIB] execute: FLOAT ADD INT: %d\n", res); } else if(instr == OP_SUB) { res = (int32_t)(fl_arg1 - (arg3->value.var * FLOAT_PRECISION)); DEBUG("[BASIC_LIB] execute: FLOAT SUB INT: %d\n", res); } else if(instr == OP_DIV) { res = (int32_t)(fl_arg1 / arg3->value.var); DEBUG("[BASIC_LIB] execute: FLOAT DIV INT: %d\n", res); sys_led(LED_RED_TOGGLE); #ifdef OUTLIER_SCRIPT_DBG int32_t* post_avg; post_avg = (int32_t*)sys_malloc(sizeof(int32_t)); *post_avg = res; sys_post_uart(OUTLIER_DETECTION_PID, MSG_AVERAGE, sizeof(int32_t), post_avg, SOS_MSG_RELEASE, UART_ADDRESS); #endif } else if(instr == OP_MUL) { res = (int32_t)(fl_arg1 * arg3->value.var); DEBUG("[BASIC_LIB] execute: FLOAT MULT INT %d\n", res); } } res_part = res & 0xFFFF; pushValue( eventState, res_part, DVM_TYPE_FLOAT_DEC); res_part = res >> 16; pushValue( eventState, res_part, DVM_TYPE_FLOAT); context->pc += 1; break; } else if (arg2->type == DVM_TYPE_FLOAT) { arg3 = popOperand( eventState); fl_arg2 = convert_to_float(arg2, arg3); if(instr == OP_ADD) { res = (int32_t)((arg1->value.var * FLOAT_PRECISION) + fl_arg2) ; DEBUG("[BASIC_LIB] execute: INT ADD FLOAT: %d\n", res); } else if(instr == OP_SUB) { res = (int32_t)((arg1->value.var * FLOAT_PRECISION) - fl_arg2); DEBUG("[BASIC_LIB] execute: INT SUB FLOAT: %d\n", res); } else if(instr == OP_DIV) { res = (int32_t)((arg1->value.var * FLOAT_PRECISION) / fl_arg2); DEBUG("[BASIC_LIB] execute: INT DIV FLOAT: %d\n", res); } else if(instr == OP_MUL) { res = (int32_t)((arg1->value.var * FLOAT_PRECISION) * fl_arg2); DEBUG("[BASIC_LIB] execute: INT MULT FLOAT: %d\n", res); } res_part = res & 0xFFFF; pushValue( eventState, res_part, DVM_TYPE_FLOAT_DEC); res_part = res >> 16; pushValue( eventState, res_part, DVM_TYPE_FLOAT); context->pc += 1; break; } if(instr == OP_ADD) { int_res = arg1->value.var + arg2->value.var; DEBUG("[BASIC_LIB] execute: INT ADD INT: %d\n", int_res); } else if(instr == OP_SUB) { int_res = arg1->value.var - arg2->value.var; DEBUG("[BASIC_LIB] execute: INT SUB INT: %d\n", int_res); } else if(instr == OP_DIV) { int_res = (int16_t)(arg1->value.var / arg2->value.var); DEBUG("[BASIC_LIB] execute: INT DIV INT: %d\n", int_res); } else if(instr == OP_MUL) { int_res = (int16_t)(arg1->value.var * arg2->value.var); DEBUG("[BASIC_LIB] execute: INT MULT INT: %d\n", int_res); } pushValue( eventState, int_res, DVM_TYPE_INTEGER); context->pc += 1; break; }
void EntityTemplateInterpreter::readString(std::vector<char>& code, int& i) { uint8_t *address = reinterpret_cast<uint8_t*>(&code[i]); pushValue(address); i += sizeof(uint8_t); }
void pushValue(std::istream* _parseSource) { std::string value; *_parseSource >> value; pushValue(value); }
void EntityTemplateInterpreter::readUint32(std::vector<char>& code, int& i) { uint32_t *val = reinterpret_cast<uint32_t*>(&code[i]); i += sizeof(uint32_t); pushValue(val); }
/* * This function is used for morph parsing. * * The value is passed to parsetext which will call the right dictionary to * lexize the word. If it turns out to be a stopword, we push a QI_VALSTOP * to the stack. * * All words belonging to the same variant are pushed as an ANDed list, * and different variants are ORred together. */ static void pushval_morph(Datum opaque, TSQueryParserState state, char *strval, int lenval, int2 weight, bool prefix) { int4 count = 0; ParsedText prs; uint32 variant, pos, cntvar = 0, cntpos = 0, cnt = 0; Oid cfg_id = DatumGetObjectId(opaque); /* the input is actually * an Oid, not a pointer */ prs.lenwords = 4; prs.curwords = 0; prs.pos = 0; prs.words = (ParsedWord *) palloc(sizeof(ParsedWord) * prs.lenwords); parsetext(cfg_id, &prs, strval, lenval); if (prs.curwords > 0) { while (count < prs.curwords) { pos = prs.words[count].pos.pos; cntvar = 0; while (count < prs.curwords && pos == prs.words[count].pos.pos) { variant = prs.words[count].nvariant; cnt = 0; while (count < prs.curwords && pos == prs.words[count].pos.pos && variant == prs.words[count].nvariant) { pushValue(state, prs.words[count].word, prs.words[count].len, weight, ((prs.words[count].flags & TSL_PREFIX) || prefix) ? true : false); pfree(prs.words[count].word); if (cnt) pushOperator(state, OP_AND); cnt++; count++; } if (cntvar) pushOperator(state, OP_OR); cntvar++; } if (cntpos) pushOperator(state, OP_AND); cntpos++; } pfree(prs.words); } else pushStop(state); }
int pushValue(lua_State* state, const std::tuple<Ts...>& tup) { return pushValue(state, tup, typename indicesBuilder<sizeof...(Ts)>::type{}); }