const char* lua_getupvalue(lua_State *L, int funcIndex, int n) { const Value* func = GetValueForIndex(L, funcIndex); luai_apicheck(L, Value_GetIsClosure(func) ); Closure* closure = func->closure; if (closure->c) { if (n >= 1 && n <= closure->cclosure.numUpValues) { PushValue(L, &closure->cclosure.upValue[n - 1]); // Up values to a C function are unnamed. return ""; } } else { if (n >= 1 && n <= closure->lclosure.numUpValues) { PushValue(L, closure->lclosure.upValue[n - 1]->value); // Get the name of the up value from the prototype. String* name = closure->lclosure.prototype->upValue[n - 1]; return String_GetData(name); } } return NULL; }
//------------------------------------------------------------------------------ void Evaluator::ApplyBinOp(Token::BinOp op) { float b = PopValue(); float a = PopValue(); switch (op) { case Token::BinOpAdd: PushValue(a + b); break; case Token::BinOpSub: PushValue(a - b); break; case Token::BinOpMul: PushValue(a * b); break; case Token::BinOpDiv: PushValue(a / b); break; default: LOG_WARN("Unknown bin-op!"); } }
void VJSArray::PushValues( const std::vector<const XBOX::VValueSingle*> inValues, JS4D::ExceptionRef *outException) const { for (std::vector<const XBOX::VValueSingle*>::const_iterator cur = inValues.begin(), end = inValues.end(); cur != end; cur++) { const VValueSingle* val = *cur; if (val == NULL) { VJSValue xval(fContext); xval.SetNull(); PushValue(xval, outException); } else PushValue(*val, outException); } }
//------------------------------------------------------------------------------ void Evaluator::LookupVar(const Token& t, const Environment* env) { const string& name = t.name; if (env) { auto it = env->constants.find(name); if (it != env->constants.end()) { PushValue(it->second); return; } } assert(constants.count(name)); PushValue(constants[name]); }
void CLR_RT_StackFrame::SetResult( CLR_INT32 val, CLR_DataType dataType ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetInteger( val, dataType ); }
static JSBool HandleKeyword(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len) { jsval keyword; JSTokenType tt = js_CheckKeyword(buf, len); if (tt != TOK_PRIMARY) return JS_FALSE; if (buf[0] == 'n') keyword = JSVAL_NULL; else if (buf[0] == 't') keyword = JSVAL_TRUE; else if (buf[0] == 'f') keyword = JSVAL_FALSE; else return JS_FALSE; jsuint length; if (!JS_GetArrayLength(cx, jp->objectStack, &length)) return JS_FALSE; jsval o; if (!JS_GetElement(cx, jp->objectStack, length - 1, &o)) return JS_FALSE; JS_ASSERT(JSVAL_IS_OBJECT(o)); JSObject *obj = JSVAL_TO_OBJECT(o); return PushValue(cx, jp, obj, keyword); }
static JSBool HandleNumber(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len) { JSBool ok; jsuint length; if (!JS_GetArrayLength(cx, jp->objectStack, &length)) return JS_FALSE; jsval o; if (!JS_GetElement(cx, jp->objectStack, length - 1, &o)) return JS_FALSE; JS_ASSERT(JSVAL_IS_OBJECT(o)); JSObject *obj = JSVAL_TO_OBJECT(o); const jschar *ep; double val; // if (!js_strtod(cx, buf, buf + len, &ep, &val) || ep != buf + len) if (!js_strtod(cx, buf, &ep, &val) || ep != buf + len) return JS_FALSE; jsval numVal; if (JS_NewNumberValue(cx, val, &numVal)) ok = PushValue(cx, jp, obj, numVal); else ok = JS_FALSE; // decode error return ok; }
static JSBool PushObject(JSContext *cx, JSONParser *jp, JSObject *obj) { jsuint len; if (!JS_GetArrayLength(cx, jp->objectStack, &len)) return JS_FALSE; if (len >= JSON_MAX_DEPTH) return JS_FALSE; // decoding error jsval v = OBJECT_TO_JSVAL(obj); // Check if this is the root object if (len == 0) { *jp->rootVal = v; if (!JS_SetElement(cx, jp->objectStack, 0, jp->rootVal)) return JS_FALSE; return JS_TRUE; } jsval p; if (!JS_GetElement(cx, jp->objectStack, len - 1, &p)) return JS_FALSE; JS_ASSERT(JSVAL_IS_OBJECT(p)); JSObject *parent = JSVAL_TO_OBJECT(p); if (!PushValue(cx, jp, parent, OBJECT_TO_JSVAL(obj))) return JS_FALSE; if (!JS_SetElement(cx, jp->objectStack, len, &v)) return JS_FALSE; return JS_TRUE; }
void CLR_RT_StackFrame::SetResult_Object( CLR_RT_HeapBlock* val ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetObjectReference( val ); }
void CLR_RT_StackFrame::SetResult_Boolean( bool val ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetBoolean( val ); }
void CLR_RT_StackFrame::SetResult_U8( CLR_UINT64& val ) { CLR_RT_HeapBlock& top = PushValue(); top.SetInteger( val ); }
void CLR_RT_StackFrame::SetResult_U4( CLR_UINT32 val ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetInteger( val ); }
void CLR_RT_StackFrame::SetResult_R8( CLR_INT64 &val ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetDouble( val ); }
void CLR_RT_StackFrame::SetResult_R4( CLR_INT32 val ) { NATIVE_PROFILE_CLR_CORE(); CLR_RT_HeapBlock& top = PushValue(); top.SetFloat( val ); }
bool VRSDClientLuaImplementation::UpdateGlobalVariable(const char* szVarName, const char* szNewValue) { if(!szVarName || !szNewValue || szVarName[0]==0) return false; // we can only get global symbols without a crash if we are really in a Lua code execution path if(strcmp(m_pActivationRecord->what, "Lua")) return true; VMemoryTempBuffer<512> copyBuffer(szVarName); // operate on a copy string in the tokenizer VLuaStackCleaner stackCleaner(m_pLuaState); ScopedBooleanToTrue disableDebugHook(m_bDebuggerRetrievingValues); VStringTokenizerInPlace Tokenizer(copyBuffer.AsChar(), '.'); const char* pCurrent = Tokenizer.Next(); unsigned int i = 0; const char* pLastField = NULL; lua_getfield(m_pLuaState, LUA_GLOBALSINDEX, pCurrent); if(lua_isnil(m_pLuaState, -1)) return false; if(LookupPath(Tokenizer, &pLastField) != HKV_SUCCESS) return false; // now the variable is at the top of the stack, update its value int iLuaType = lua_type(m_pLuaState, -1); lua_pop(m_pLuaState, 1); bool bIsIntegerKey = false; if(pLastField && VStringUtil::IsIntegerString(pLastField)) { bIsIntegerKey = true; lua_pushnumber(m_pLuaState, (LUA_NUMBER)atoi(pLastField)); } if (!PushValue(iLuaType, szNewValue)) return false; if( Tokenizer.GetTokenCount() > 1 ) { VASSERT(pLastField != NULL); if(bIsIntegerKey) { lua_settable(m_pLuaState, -3); } else { lua_setfield(m_pLuaState, -2, pLastField); } } else { lua_setglobal(m_pLuaState, szVarName); } return true; }
bool LuaTable::KeyExists(const string& key) const { if (!PushValue(key)) { return false; } lua_pop(L, 1); return true; }
// ================================================================================= // Push Memory // ================================================================================= void ScriptHook::PushMemory(ScriptBinds::Memory::MemoryBlock* pMemBlock) { if (pMemBlock == NULL || !pMemBlock->IsValid()) { lua->PushString("ScriptHook::PushMemory failed! Invalid CMemoryBlock passed!"); throw luabind::error(lua->State()); } PushValue(pMemBlock->GetMemoryPointer()); }
int PT_Get (lua_State *L) { ParamTable *pt = check_PT (L, 1); const char *key = luaL_checkstring (L, 2); luaL_argcheck (L, key, 2, "a valid string expected"); DEBUG ("PT_Get: %s\n", key); const Value *val = pt->Get (key); PushValue (L, val); return 1; }
int LuaTable::GetLength(const string& key) const { if (!PushValue(key)) { return 0; } const int len = lua_objlen(L, -1); lua_pop(L, 1); return len; }
int LuaTable::GetType(const string& key) const { if (!PushValue(key)) { return -1; } const int type = lua_type(L, -1); lua_pop(L, 1); return type; }
// methods void IntegerClass::Abs(Value &self, Value* execution_stack, size_t &execution_stack_pos, INT_T arg_count) { if(self.type != INT_TYPE || arg_count != 0) { wcerr << L">>> expected integer type <<<" << endl; exit(1); } Value value(INT_TYPE); value.value.int_value = labs(self.value.int_value); PushValue(value, execution_stack, execution_stack_pos); }
static JSBool HandleString(JSContext *cx, JSONParser *jp, const jschar *buf, uint32 len) { JSObject *obj = GetTopOfObjectStack(cx, jp); JSString *str = js_NewStringCopyN(cx, buf, len); if (!obj || !str) return JS_FALSE; return PushValue(cx, jp, obj, STRING_TO_JSVAL(str)); }
HRESULT CLR_RT_StackFrame::SetResult_String( LPCSTR val ) { NATIVE_PROFILE_CLR_CORE(); TINYCLR_HEADER(); CLR_RT_HeapBlock& top = PushValue(); TINYCLR_SET_AND_LEAVE(CLR_RT_HeapBlock_String::CreateInstance( top, val )); TINYCLR_NOCLEANUP(); }
bool VRSDClientLuaImplementation::UpdateDynamicProperty(const void* pUserDataParent, const char* szVarName, const char* szNewValue) { VASSERT(m_pLuaState); lua_State* L = m_pLuaState; VLuaStackCleaner stackCleaner(L); VTypedObject* pTypedObject = LUA_ExtractFromUserData(L, pUserDataParent); if (pTypedObject == NULL) { return false; } LUA_LookupObjectProxy(L, pTypedObject); // proxy or nil, TOP if (lua_isnil(L, -1)) { return false; } LUA_FetchDynPropertyTable(L); // proxy, dynproptable or nil, TOP if (lua_isnil(L, -1)) { return false; } lua_pushstring(L, szVarName); // proxy, dynproptable, key, TOP lua_pushvalue(L, -1); // proxy, dynproptable, key, key, TOP lua_rawget(L, -3); // proxy, dynproptable, key, value, TOP if (lua_isnil(L, -1)) { return false; } int iType = lua_type(L, -1); // proxy, dynproptable, key, value, TOP lua_pop(L, 1); // proxy, dynproptable, key, TOP if (!PushValue(iType, szNewValue)) // proxy, dynproptable, key, newvalue, TOP { return false; } lua_rawset(L, -3); return true; }
void StringClass::Size(Value &self, Value* execution_stack, size_t &execution_stack_pos, INT_T arg_count) { if(self.type != STRING_TYPE || arg_count != 0) { wcerr << L">>> expected string type <<<" << endl; exit(1); } Value value(INT_TYPE); value.sys_klass = IntegerClass::Instance(); Value* self_value = static_cast<Value*>(self.value.ptr_value); value.value.int_value = (INT_T)static_cast<wstring*>(self_value->value.ptr_value)->size(); PushValue(value, execution_stack, execution_stack_pos); }
string LuaTable::GetString(int key, const string& def) const { if (!PushValue(key)) { return def; } if (!lua_isstring(L, -1)) { lua_pop(L, 1); return def; } const string value = lua_tostring(L, -1); lua_pop(L, 1); return value; }
float3 LuaTable::GetFloat3(int key, const float3& def) const { if (!PushValue(key)) { return def; } float3 value; if (!ParseFloat3(L, -1, value)) { lua_pop(L, 1); return def; } lua_pop(L, 1); return value; }
float LuaTable::GetFloat(int key, float def) const { if (!PushValue(key)) { return def; } if (!lua_isnumber(L, -1)) { lua_pop(L, 1); return def; } const float value = lua_tofloat(L, -1); lua_pop(L, 1); return value; }
bool LuaTable::GetBool(int key, bool def) const { if (!PushValue(key)) { return def; } bool value; if (!ParseBoolean(L, -1, value)) { lua_pop(L, 1); return def; } lua_pop(L, 1); return value; }
int LuaTable::GetInt(int key, int def) const { if (!PushValue(key)) { return def; } if (!lua_isnumber(L, -1)) { lua_pop(L, 1); return def; } const int value = lua_toint(L, -1); lua_pop(L, 1); return value; }