void test1() { printf("Test 1: Objects on stack are preserved.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); gc(vm); assert(vm->numObjects == 2, "Should have preserved objects."); freeVM(vm); }
void test2() { printf("Test 2: Unreached objects are collected.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); pop(vm); pop(vm); gc(vm); assert(vm->numObjects == 0, "Should have collected objects."); freeVM(vm); }
void GameNetDelegate::onMessageReceived(const char *data, unsigned short size) { CCLOG("onMessageReceived, size is %d %d", size, size - sizeof(CMD_Head)); CMD_Command Command = ((CMD_Head *)data)->CommandInfo; auto stack = LuaEngine::getInstance()->getLuaStack(); stack->pushInt(Command.wMainCmdID); stack->pushInt(Command.wSubCmdID); stack->pushString(data + sizeof(CMD_Head), size - sizeof(CMD_Head)); auto it = m_callBacks.find("onReceived"); stack->executeFunctionByHandler(it->second, 3); stack->clean(); }
LUALIB_API int luaopen_int64(lua_State *L) { if (sizeof(Int)<8) luaL_error(L,"int64 cannot work with %d-byte values",sizeof(Int)); luaL_newmetatable(L,MYTYPE); luaL_setfuncs(L,R,0); lua_pushliteral(L,"version"); /** version */ lua_pushliteral(L,MYVERSION); lua_settable(L,-3); pushInt(L,LLONG_MIN); lua_setfield(L,-2,"min"); /** min */ pushInt(L,LLONG_MAX); lua_setfield(L,-2,"max"); /** max */ return 1; }
void BenchmarkTcpClient::buildWriteChunk() { if (writeBuffer.empty()) { if (server) { if (answerSizeRemain > 0) { int size = std::min(answerSizeRemain, 1024); writeBuffer.reserve(size); for(char *c = (char*)writeBuffer.end(), *end = c + size; c != end; ++c) *c = (int)random(currentSeed)%256 - 128; writeBuffer.push(size); answerSizeRemain -= size; eventWrite.setTimeRelativeNow(); if (answerSizeRemain <= 0) { requestSize = -1; answerSize = -1; } } } else { int size = std::min(requestSize + 3*(int)sizeof(int), 1024); writeBuffer.reserve(size); if (requestsCountRemain == requestsCount) { pushInt(requestsCount); requestSizeRemain = requestSize; } if (requestSizeRemain == requestSize) { if (requestsCountRemain <= 0 || requestSize <= 0 || answerSize <= 0) { formatError(); return; } pushInt(requestSize); pushInt(answerSize); --requestsCountRemain; } if (requestSizeRemain > 0) { size -= writeBuffer.size(); size = std::min(requestSizeRemain, size); if (size > 0) { for(char *c = (char*)writeBuffer.end(), *end = c + size; c != end; ++c) *c = (int)random(currentSeed)%256 - 128; currentCrc32 = Packet::crc32(writeBuffer.end(), size, currentCrc32); writeBuffer.push(size); requestSizeRemain -= size; } if (requestSizeRemain <= 0) { answerSizeRemain = answerSize; currentSeed = currentCrc32; currentCrc32 = 0; } eventWrite.setTimeRelativeNow(); } } } }
void test3() { printf("Test 3: Reach nested objects.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); pushPair(vm); pushInt(vm, 3); pushInt(vm, 4); pushPair(vm); pushPair(vm); gc(vm); assert(vm->numObjects == 7, "Should have reached objects."); freeVM(vm); }
void Interpreter::divInts() { int64_t right = popInt(); int64_t left = popInt(); //cout << "Dividing " << left << " " << right << endl; pushInt(left / right); }
void Interpreter::iMul() { int64_t i1 = popInt(); int64_t i2 = popInt(); //cout << "Multiplying " << i1 << " " << i2 << endl; pushInt(i1*i2); }
void Interpreter::subInts() { int64_t top = popInt(); int64_t down = popInt(); int64_t sub = top - down; pushInt(sub); }
void Interpreter::loadCtxInt() { FunctionContext* ctx = topContext(); uint16_t ctxId = getNext2Bytes(); uint16_t id = getNext2Bytes(); pushInt(ctx->readInt(ctxId, id)); }
LANDRU_DECL_FN(ColorVarObj, getInt) { ColorVarObj* o = (ColorVarObj*) p->vo.get(); p->stack->pop(); pushInt(p, (int) o->c); }
int CCLuaEngine::pushCCLuaValue(const CCLuaValue& value) { const CCLuaValueType type = value.getType(); if (type == CCLuaValueTypeInt) { return pushInt(value.intValue()); } else if (type == CCLuaValueTypeFloat) { return pushFloat(value.floatValue()); } else if (type == CCLuaValueTypeBoolean) { return pushBoolean(value.booleanValue()); } else if (type == CCLuaValueTypeString) { return pushString(value.stringValue().c_str()); } else if (type == CCLuaValueTypeDict) { pushCCLuaValueDict(value.dictValue()); } else if (type == CCLuaValueTypeArray) { pushCCLuaValueArray(value.arrayValue()); } else if (type == CCLuaValueTypeCCObject) { pushCCObject(value.ccobjectValue(), value.getCCObjectTypename().c_str()); } return lua_gettop(m_state); }
void DBCCArmatureNode::registerMovementEventHandler(cocos2d::LUA_FUNCTION func) { unregisterMovementEventHandler(); _movementEventHandler = func; auto dispatcher = getCCEventDispatcher(); auto f = [this](cocos2d::EventCustom *event) { auto eventData = (dragonBones::EventData*)(event->getUserData()); auto type = (int) eventData->getType(); auto movementId = eventData->animationState->name; auto lastState = eventData->armature->getAnimation()->getLastAnimationState(); auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack(); stack->pushObject(this, "db.DBCCArmatureNode"); stack->pushInt(type); stack->pushString(movementId.c_str(), movementId.size()); stack->pushBoolean(lastState == eventData->animationState); stack->executeFunctionByHandler(_movementEventHandler, 4); }; dispatcher->addCustomEventListener(dragonBones::EventData::COMPLETE, f); dispatcher->addCustomEventListener(dragonBones::EventData::LOOP_COMPLETE, f); }
void LuaStack::pushLuaValue(const LuaValue& value) { const LuaValueType type = value.getType(); if (type == LuaValueTypeInt) { return pushInt(value.intValue()); } else if (type == LuaValueTypeFloat) { return pushFloat(value.floatValue()); } else if (type == LuaValueTypeBoolean) { return pushBoolean(value.booleanValue()); } else if (type == LuaValueTypeString) { return pushString(value.stringValue().c_str()); } else if (type == LuaValueTypeDict) { pushLuaValueDict(value.dictValue()); } else if (type == LuaValueTypeArray) { pushLuaValueArray(value.arrayValue()); } else if (type == LuaValueTypeObject) { pushObject(value.ccobjectValue(), value.getObjectTypename().c_str()); } }
void autoMatchResponse(const boids::MatchResponse& response) { auto stack = cocos2d::LuaEngine::getInstance()->getLuaStack(); stack->pushInt(response.ret_value()); stack->pushString(response.ret_info().c_str()); if (response.ret_value() == boids::MatchResponse_Value_Success) { cocos2d::log("autoMatch success. ip: %s, port: %d", response.game_server_ip().c_str(), response.game_server_port()); if (NetworkAdapter::getInstance()->init(response.game_server_ip(), response.game_server_port())) { processGameInit(response.game_init_data(), stack); stack->executeFunctionByHandler(autoMatchCallback, 3); } else { cocos2d::log("[ERROR] udp init failed !"); } } else { cocos2d::log("autoMatch error: %d %s", response.ret_value(), response.ret_info().c_str()); stack->executeFunctionByHandler(autoMatchCallback, 2); } }
//-------------------------------------------------------------------------------- // aload_0 int op_aload_0( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { pushInt(stack, 0); #if SIMPLE_JVM_DEBUG printf("push 0 into stack\n"); #endif *opCode = *opCode + 1; return 0; }
// iconst_5 int op_iconst_5( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { pushInt(stack, 5); #if SIMPLE_JVM_DEBUG printf("iconst_5: push 5 into stack\n"); #endif *opCode = *opCode + 1; return 0; }
int luaopen_int64(lua_State *L) { if (sizeof(Int)<8) luaL_error(L,"int64 cannot work with %d-byte values",sizeof(Int)); luaL_newmetatable(L, MYTYPE); /*stack: mt*/ lua_setglobal(L, MYNAME); /*_G[MYNAME] = mt stack: */ luaL_register(L, MYNAME, R); /*_G[MYNAME].__add = ..., _G[MYNAME].__div = ldiv.. stack: mt*/ lua_pushliteral(L,"version"); /** version */ lua_pushliteral(L, MYVERSION); lua_settable(L,-3); /*mt[version] = MYVERSION, stack:mt*/ pushInt(L,LLONG_MIN); lua_setfield(L,-2,"min"); /** min */ pushInt(L,LLONG_MAX); lua_setfield(L,-2,"max"); /** max */ lua_pop(L, 1); return 0; }
void test4() { printf("Test 4: Handle cycles.\n"); VM* vm = newVM(); pushInt(vm, 1); pushInt(vm, 2); Object* a = pushPair(vm); pushInt(vm, 3); pushInt(vm, 4); Object* b = pushPair(vm); a->tail = b; b->tail = a; gc(vm); assert(vm->numObjects == 4, "Should have collected objects."); freeVM(vm); }
void Interpreter::addInts() { int64_t top = popInt(); int64_t down = popInt(); int64_t sum = top + down; pushInt(sum); //cout << "adding ints" << endl; }
//iload_3 int op_iload_3( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { int value = localVariables.integer[3]; #if SIMPLE_JVM_DEBUG printf("iload_3: load value from local variable 3(%d)\n",localVariables.integer[3]); #endif pushInt(stack, value); *opCode = *opCode + 1; return 0; }
/* * Execute the GETSTATIC instruction */ void op_getstatic() { Ref field = resolve(u16(1), ID_FIELD); if (field == NULL) { return; } // rollback // read field values Ref type = getRef(field, ENTRY_OWNER); Ref statics = getRef(type, TYPE_STATICS); Int index = getInt(field, FIELD_INDEX); Int flag = getInt(field, FIELD_REFERENCE_FLAG); Int size = getInt(field, FIELD_SIZE); int offset = STATICS_FIELDS + index; // read values and push on stack if (flag) { pushRef(getRef(statics, offset)); } else { pushInt(getInt(statics, offset)); } if (size == 2) { pushInt(getInt(statics, offset + 1)); } pc += 3; }
//bipush int op_bipush( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { int value = opCode[0][1]; pushInt(stack, value); #if SIMPLE_JVM_DEBUG printf("push a byte %d onto the stack \n", value); #endif *opCode = *opCode + 2; return 0; }
/* * Execute the FCMPG instruction */ void op_fcmpg() { float f2 = popFloat(); float f1 = popFloat(); int k = 1; if (f1 < f2) { k = -1; } if (f1 == f2) { k = 0; } pushInt(k); pc++; }
/* * Execute the LCMP instruction */ void op_lcmp() { DoubleWord value2 = popDoubleWord(); DoubleWord value1 = popDoubleWord(); int result = 0; if (value1.l > value2.l) { result = 1; } else if (value1.l < value2.l) { result = -1; } pushInt(result); pc++; }
//dup int op_dup( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { StackEntry *entry = popEntry(stack); int value = 0; value = EntryToInt(entry); if ( entry->type == STACK_ENTRY_INT ) { pushInt(stack, value); pushInt(stack, value); } else { pushRef(stack, value); pushRef(stack, value); } #if SIMPLE_JVM_DEBUG printf("dup\n"); #endif *opCode = *opCode + 1; return 0; }
//iload int op_iload( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { int index = opCode[0][1]; int value = localVariables.integer[index]; #if SIMPLE_JVM_DEBUG printf("iload: load value from local variable %d(%d)\n", index, localVariables.integer[index]); #endif pushInt(stack, value); *opCode = *opCode + 2; return 0; }
/* * Execute the INSTANCEOF instruction */ void op_instanceof() { Ref entry = getPoolEntry(u16(1)); Ref object = popRef(); int value = FALSE; if (object != NULL) { Ref type = getRef(object, OBJECT_TYPE); value = isSubClassOrImplementationOf(type, entry); } pushInt(value); pc += 3; }
/* * Execute the GETFIELD instruction */ void op_getfield() { Ref field = resolve(u16(1), ID_FIELD); if (field == NULL) { return; } // rollback // check for null pointer Ref object = popRef(); if (object == NULL) { throwException(CORE_THROW_NULL_POINTER); return; } // push value Int index = getInt(field, FIELD_INDEX) + OBJECT_FIELDS; Int flag = getInt(field, FIELD_REFERENCE_FLAG); Int size = getInt(field, FIELD_SIZE); if (flag) { pushRef(getRef(object, index)); } else { pushInt(getInt(object, index)); } if (size == 2) { pushInt(getInt(object, index + 1)); } pc += 3; }
//0x8e d2i int op_d2i( unsigned char **opCode, StackFrame *stack, SimpleConstantPool *p ) { double value1 = popDouble(stack); int result = 0; result = (int)value1; #if SIMPLE_JVM_DEBUG printf("d2i: %d <-- %f\n", result, value1); #endif pushInt(stack, result); *opCode = *opCode + 1; return 0; }