void codegen_close_asm(void) { struct variable *var; var = get_global("main"); if ((var) && (var->node->tag == node_proc)) { /* a procedure named "main" exists so add the startup code */ codegen_write_comment("startup and interrupt vectors"); fprintf(state.output.f, "RESET_VECTOR code 0x0\n"); codegen_line_number(var->node); fprintf(state.output.f, " movlw high %s\n", var->name); fprintf(state.output.f, " movwf PCLATH\n"); fprintf(state.output.f, " goto %s\n\n", var->name); } var = get_global("isr"); if ((var) && (var->node->tag == node_proc)) { fprintf(state.output.f, "INT_VECTOR code 0x4\n"); codegen_write_comment("store wreg and status"); fprintf(state.output.f, " movwf w_temp\n"); fprintf(state.output.f, " movf STATUS, w\n"); fprintf(state.output.f, " movwf status_temp\n"); fprintf(state.output.f, " movf PCLATH, w\n"); fprintf(state.output.f, " movwf pclath_temp\n"); codegen_write_comment("call the interrupt routine"); fprintf(state.output.f, " pagesel %s\n", var->name); fprintf(state.output.f, " call %s\n", var->name); codegen_write_comment("restore wreg and status"); fprintf(state.output.f, " movf pclath_temp, w\n"); fprintf(state.output.f, " movwf PCLATH\n"); fprintf(state.output.f, " movf status_temp, w\n"); fprintf(state.output.f, " movwf STATUS\n"); fprintf(state.output.f, " swapf w_temp, f\n"); fprintf(state.output.f, " swapf w_temp, w\n"); fprintf(state.output.f, " retfie\n\n"); fprintf(state.output.f, "INT_VAR udata_shr\n"); codegen_write_comment("FIXME: not all processors have shared memory"); fprintf(state.output.f, "w_temp res 1\n"); fprintf(state.output.f, "status_temp res 1\n\n"); fprintf(state.output.f, "pclath_temp res 1\n\n"); } fprintf(state.output.f, "#eof\n\n"); fprintf(state.output.f, " end\n\n"); fclose(state.output.f); return; }
int main(int, char**) { assert(make_calls == 0); assert(get_calls == 0); int* a1 = get_global(); assert(make_calls == 1); assert(get_calls == 1); int* a2 = get_global(); assert(make_calls == 1); assert(get_calls == 2); assert(a1 == &dummy); assert(a2 == &dummy); return 0; }
static void gen_expr(tree *expr) { struct variable *var; switch(expr->tag) { case node_arg: var = get_global(ARG_NAME(expr)); codegen_load_file(expr, var); break; case node_call: analyze_call(expr, true, codegen_size); break; case node_constant: LOAD_CONSTANT(expr->value.constant, codegen_size); break; case node_symbol: var = get_global(SYM_NAME(expr)); if (var->tag == sym_const) { LOAD_CONSTANT(var->value, codegen_size); } else { codegen_load_file(expr, var); } break; case node_unop: gen_unop_expr(expr); break; case node_binop: if (expr->value.binop.op == op_assign) { analyze_error(expr, "assign operator = should be equal operator =="); } else if ((expr->value.binop.op == op_lsh) || (expr->value.binop.op == op_rsh)) { /* for shifts it is best to calculate the left side first */ gen_binop_expr(expr->value.binop.op, expr->value.binop.p1, expr->value.binop.p0); } else { /* for all others calculate the right side first */ gen_binop_expr(expr->value.binop.op, expr->value.binop.p0, expr->value.binop.p1); } break; default: assert(0); } }
/* ------------------------------------------------------------------------- */ PLUGIN_MENU_PUBLIC_API PLUGIN_INIT() { struct plugin_t* plugin; /* init global data */ glob_create(game); /* init plugin */ plugin = plugin_create(game, PLUGIN_NAME, PLUGIN_CATEGORY, PLUGIN_AUTHOR, PLUGIN_DESCRIPTION, PLUGIN_WEBSITE ); get_global(game)->plugin = plugin; /* set plugin information - Change this in the file "CMakeLists.txt" */ plugin_set_programming_language(plugin, PLUGIN_PROGRAMMING_LANGUAGE_C ); plugin_set_version(plugin, PLUGIN_VERSION_MAJOR, PLUGIN_VERSION_MINOR, PLUGIN_VERSION_PATCH ); register_services(plugin); register_events(plugin); return plugin; }
double get_global_checked(int gi) const { if (is_gindex(gi)) { return get_global(gi); } else { throw std::out_of_range("element not on this process."); } }
void _handle_special_chatter(creature_t* creature) { std::string name = creature->name; if (name.find("Harmund") != std::string::npos) { if (!global_is_set("necromancer_killed")) { append_msg_log("%s grunts, \"Greetings to ye, adventurer. Lately our quiet town has been terrorized by an evil necromancer.", capitalize(creature->get_full_name()).c_str()); append_msg_log("Ye would offer us a great service if ye could find and deal with him... Ye can find his hideout in the caverns to the east of here...\""); } else if (get_global("necromancer_killed") == "true" && !global_is_set("harmund_granted_item")) { append_msg_log("%s smiles at you, \"Ah, adventurer. I have heard the good news! The necromancer is dead and we can all rejoice, for now.", capitalize(creature->get_full_name()).c_str()); append_msg_log("Please take this as a token of our appreciation.\""); set_global("harmund_granted_item", "true"); } else { append_msg_log("%s smiles at you, \"Greetings once again, adventurer...\"", capitalize(creature->get_full_name()).c_str()); } } }
static void gen_binop_expr(tree *expr) { tree *lhs = expr->value.binop.p0; tree *rhs = expr->value.binop.p1; /* calculate right hand side */ gen_expr(rhs); if (lhs->tag == node_constant) { gen_binop_constant(expr->value.binop.op, lhs->value.constant); } else if (lhs->tag == node_symbol) { struct variable *var; var = get_global(lhs->value.symbol); if (var->is_constant) { gen_binop_constant(expr->value.binop.op, var->value); } else { gen_binop_symbol(expr->value.binop.op, var->alias); } } else { char *temp_name; /* it is a complex expression so save temp data */ temp_name = codegen_get_temp(); codegen_write_asm("movwf %s", temp_name); gen_expr(lhs); gen_binop_symbol(expr->value.binop.op, temp_name); if (temp_name) free(temp_name); } }
void RegExpObject::fixReplaceLastIndex(const char *src, int subjectLength, int lastIndex, int& newLastIndex, StringBuffer& resultBuffer) { if (lastIndex == newLastIndex && get_global()) { // Advance one character if (lastIndex < subjectLength) { uint32 ch; int n = UnicodeUtils::Utf8ToUcs4((const uint8*)src+lastIndex, subjectLength-lastIndex, &ch); if (n <= 0) { // Invalid UTF8 sequence, advance one byte n = 1; } resultBuffer.write(src+lastIndex, n); newLastIndex += n; } else { newLastIndex++; } } }
void gen_expr(tree *expr) { struct variable *var; switch(expr->tag) { case node_constant: gen_immed(expr->value.constant); break; case node_symbol: var = get_global(expr->value.symbol); if (var->is_constant) { codegen_write_asm("movlw %i", var->value); } else { gen_get_mem(var->alias); } break; case node_unop: gen_unop_expr(expr); break; case node_binop: gen_binop_expr(expr); break; default: print_node(expr, 0); assert(0); } }
void *threadfunc(void *arg) { int i = (int)arg; global = i; //pthread_setspecific(key, &global); get_global(); }
int su_getglobal(su_state *s, const char *name) { int size = strlen(name); value_t v = get_global(s, name, murmur(name, size, 0), size); if (v.type == SU_INV) return 0; push_value(s, &v); return 1; }
void register_events(struct plugin_t* plugin) { /* get events struct and initialise all event pointers to NULL */ struct glob_events_t* g = &get_global(plugin->game)->events; memset(g, 0, sizeof(struct glob_events_t)); EVENT_CREATE0(plugin, g->button_clicked, PLUGIN_NAME ".button_clicked"); }
/* ------------------------------------------------------------------------- */ PLUGIN_MENU_PUBLIC_API PLUGIN_STOP() { struct glob_t* g; /* de-init */ g = get_global(game); menu_deinit(g); button_deinit(g); }
/* ------------------------------------------------------------------------- */ PLUGIN_MENU_PUBLIC_API PLUGIN_START() { struct glob_t* g = get_global(game); if(!get_required_services(g->plugin)) return PLUGIN_FAILURE; get_optional_services(g->plugin); register_event_listeners(g->plugin); element_init(g); button_init(g); menu_init(g); return PLUGIN_SUCCESS; }
ArrayObject* RegExpObject::_exec(Stringp subject, UTF8String *utf8Subject) { AvmAssert(subject != NULL); AvmAssert(utf8Subject != NULL); int matchIndex = 0, matchLen = 0; int startIndex = (get_global() ? Utf16ToUtf8Index(subject, utf8Subject, m_lastIndex) : 0); ArrayObject* result = _exec(subject, utf8Subject, startIndex, matchIndex, matchLen); if (get_global()) { m_lastIndex = Utf8ToUtf16Index(subject, utf8Subject, matchIndex+matchLen); } return result; }
void testCritical (void) { #if defined(__SDCC_mcs51) big x; unsigned char i; //enable the interrupt and set it ET2 = 1; EA = 1; TF2 = 1; __critical x.a = global_var.a; ASSERT (x.b == x.c); x.a = get_global (); ASSERT (x.b == x.c); for (i = 10; i != 0; i--) { __critical x.a = global_var.a; ASSERT (x.b == x.c); x.a = get_global(); ASSERT (x.b == x.c); } //check the interrupt has run at all ASSERT (x.a != 0); __critical y = 0; //check the interrupts are still enabled ASSERT (EA); #else ASSERT (1); #endif }
void player::notify_key_object(key::code k, bool down) { as_value kval; as_object* global = get_global(); global->get_member("Key", &kval); as_key* ko = cast_to<as_key>(kval.to_object()); if (ko) { if (down) ko->set_key_down(k); else ko->set_key_up(k); } else { log_error("gameswf::notify_key_event(): no Key built-in\n"); } }
static void gen_binop_expr(enum node_op op, tree *p0, tree *p1) { char *reg1 = NULL; char *reg2 = NULL; struct variable *var; gen_expr(p1); if (p0->tag == node_call) { reg1 = codegen_get_temp(codegen_size); STORE_FILE(reg1, codegen_size, 0, false); analyze_call(p0, true, codegen_size); CODEGEN(op, codegen_size, false, 0, reg1); } else if (p0->tag == node_constant) { CODEGEN(op, codegen_size, true, p0->value.constant, NULL); } else if (p0->tag == node_symbol) { var = get_global(SYM_NAME(p0)); if (var->tag == sym_const) { CODEGEN(op, codegen_size, true, var->value, NULL); } else if (SYM_OFST(p0)) { /* it is a complex expression, so save temp data */ reg1 = codegen_get_temp(codegen_size); reg2 = codegen_get_temp(codegen_size); STORE_FILE(reg1, codegen_size, 0, false); codegen_load_file(p0, var); STORE_FILE(reg2, codegen_size, 0, false); LOAD_FILE(reg1, codegen_size, 0, false); CODEGEN(op, codegen_size, false, 0, reg2); } else { CODEGEN(op, codegen_size, false, 0, var->name); } } else { /* it is a complex expression so save temp data */ reg1 = codegen_get_temp(codegen_size); STORE_FILE(reg1, codegen_size, 0, false); gen_expr(p0); CODEGEN(op, codegen_size, false, 0, reg1); } if (reg1) free(reg1); if (reg2) free(reg2); }
ArrayObject* RegExpObject::match(Stringp subject) { UsesUTF8String utf8Subject(subject); if (!get_global()) { return _exec(subject, utf8Subject); } else { ArrayObject *a = toplevel()->arrayClass->newArray(); int oldLastIndex = m_lastIndex; m_lastIndex = 0; int n = 0; ArrayObject* matchArray; while (true) { int last = m_lastIndex; int matchIndex = 0, matchLen = 0; int startIndex = Utf16ToUtf8Index(subject, utf8Subject, m_lastIndex); matchArray = _exec(subject, utf8Subject, startIndex, matchIndex, matchLen); m_lastIndex = Utf8ToUtf16Index(subject, utf8Subject, matchIndex+matchLen); if ((matchArray == NULL) || (last == m_lastIndex)) break; a->setUintProperty(n++, matchArray->getUintProperty(0)); } if (m_lastIndex == oldLastIndex) { m_lastIndex++; } return a; } }
static void write_expression(tree *statement) { tree *lhs; tree *rhs; struct variable *var; char *lhs_name; fprintf(state.output.f, ";#CSRC %s %d\n", state.srcfilename, statement->line_number); if ((statement->tag != node_binop) || (statement->value.binop.op != '=')) { gp_error("invalid expression"); return; } lhs = statement->value.binop.p0; rhs = statement->value.binop.p1; if (lhs->tag != node_symbol) { gp_error("invalid lvalue in assignment"); return; } var = get_global(lhs->value.symbol); lhs_name = var->alias; temp_number = 0; gen_expr(rhs); if (var) gen_put_mem(lhs_name); if (temp_number > max_temp_number) max_temp_number = temp_number; }
/* Called when a command is invoked */ TEE_Result TA_InvokeCommandEntryPoint(void *pSessionContext, uint32_t nCommandID, uint32_t nParamTypes, TEE_Param pParams[4]) { static bool use_fptr = false; (void)pSessionContext; switch (nCommandID) { case TA_CRYPT_CMD_SHA224: use_fptr = !use_fptr; if (use_fptr) return ta_cmd_entries[nCommandID](nParamTypes, pParams); else return ta_entry_sha224(nParamTypes, pParams); case TA_CRYPT_CMD_SHA256: use_fptr = !use_fptr; if (use_fptr) return ta_cmd_entries[nCommandID](nParamTypes, pParams); else return ta_entry_sha256(nParamTypes, pParams); case TA_CRYPT_CMD_AES256ECB_ENC: return ta_entry_aes256ecb_encrypt(nParamTypes, pParams); case TA_CRYPT_CMD_AES256ECB_DEC: return ta_entry_aes256ecb_decrypt(nParamTypes, pParams); case TA_CRYPT_CMD_ALLOCATE_OPERATION: return ta_entry_allocate_operation(nParamTypes, pParams); case TA_CRYPT_CMD_FREE_OPERATION: return ta_entry_free_operation(nParamTypes, pParams); case TA_CRYPT_CMD_GET_OPERATION_INFO: return ta_entry_get_operation_info(nParamTypes, pParams); case TA_CRYPT_CMD_RESET_OPERATION: return ta_entry_reset_operation(nParamTypes, pParams); case TA_CRYPT_CMD_SET_OPERATION_KEY: return ta_entry_set_operation_key(nParamTypes, pParams); case TA_CRYPT_CMD_SET_OPERATION_KEY2: return ta_entry_set_operation_key2(nParamTypes, pParams); case TA_CRYPT_CMD_COPY_OPERATION: return ta_entry_copy_operation(nParamTypes, pParams); case TA_CRYPT_CMD_DIGEST_UPDATE: return ta_entry_digest_update(nParamTypes, pParams); case TA_CRYPT_CMD_DIGEST_DO_FINAL: return ta_entry_digest_do_final(nParamTypes, pParams); case TA_CRYPT_CMD_CIPHER_INIT: return ta_entry_cipher_init(nParamTypes, pParams); case TA_CRYPT_CMD_CIPHER_UPDATE: return ta_entry_cipher_update(nParamTypes, pParams); case TA_CRYPT_CMD_CIPHER_DO_FINAL: return ta_entry_cipher_do_final(nParamTypes, pParams); case TA_CRYPT_CMD_MAC_INIT: return ta_entry_mac_init(nParamTypes, pParams); case TA_CRYPT_CMD_MAC_UPDATE: return ta_entry_mac_update(nParamTypes, pParams); case TA_CRYPT_CMD_MAC_FINAL_COMPUTE: return ta_entry_mac_final_compute(nParamTypes, pParams); case TA_CRYPT_CMD_MAC_FINAL_COMPARE: return ta_entry_mac_final_compare(nParamTypes, pParams); case TA_CRYPT_CMD_ALLOCATE_TRANSIENT_OBJECT: return ta_entry_allocate_transient_object(nParamTypes, pParams); case TA_CRYPT_CMD_FREE_TRANSIENT_OBJECT: return ta_entry_free_transient_object(nParamTypes, pParams); case TA_CRYPT_CMD_RESET_TRANSIENT_OBJECT: return ta_entry_reset_transient_object(nParamTypes, pParams); case TA_CRYPT_CMD_POPULATE_TRANSIENT_OBJECT: return ta_entry_populate_transient_object(nParamTypes, pParams); case TA_CRYPT_CMD_COPY_OBJECT_ATTRIBUTES: return ta_entry_copy_object_attributes(nParamTypes, pParams); case TA_CRYPT_CMD_GENERATE_KEY: return ta_entry_generate_key(nParamTypes, pParams); case TA_CRYPT_CMD_ASYMMETRIC_ENCRYPT: return ta_entry_asymmetric_encrypt(nParamTypes, pParams); case TA_CRYPT_CMD_ASYMMETRIC_DECRYPT: return ta_entry_asymmetric_decrypt(nParamTypes, pParams); case TA_CRYPT_CMD_ASYMMETRIC_SIGN_DIGEST: return ta_entry_asymmetric_sign_digest(nParamTypes, pParams); case TA_CRYPT_CMD_ASYMMETRIC_VERIFY_DIGEST: return ta_entry_asymmetric_verify_digest(nParamTypes, pParams); case TA_CRYPT_CMD_DERIVE_KEY: return ta_entry_derive_key(nParamTypes, pParams); case TA_CRYPT_CMD_RANDOM_NUMBER_GENEREATE: return ta_entry_random_number_generate(nParamTypes, pParams); case TA_CRYPT_CMD_AE_INIT: return ta_entry_ae_init(nParamTypes, pParams); case TA_CRYPT_CMD_AE_UPDATE_AAD: return ta_entry_ae_update_aad(nParamTypes, pParams); case TA_CRYPT_CMD_AE_UPDATE: return ta_entry_ae_update(nParamTypes, pParams); case TA_CRYPT_CMD_AE_ENCRYPT_FINAL: return ta_entry_ae_encrypt_final(nParamTypes, pParams); case TA_CRYPT_CMD_AE_DECRYPT_FINAL: return ta_entry_ae_decrypt_final(nParamTypes, pParams); case TA_CRYPT_CMD_GET_OBJECT_BUFFER_ATTRIBUTE: return ta_entry_get_object_buffer_attribute(nParamTypes, pParams); case TA_CRYPT_CMD_GET_OBJECT_VALUE_ATTRIBUTE: return ta_entry_get_object_value_attribute(nParamTypes, pParams); case TA_CRYPT_CMD_SETGLOBAL: return set_global(nParamTypes, pParams); case TA_CRYPT_CMD_GETGLOBAL: return get_global(nParamTypes, pParams); default: return TEE_ERROR_BAD_PARAMETERS; } }
void as_s_function::operator()(const fn_call& fn) // Dispatch. { assert(fn.env); // Keep target alive during execution! smart_ptr<as_object> target = m_target; // try to use caller environment // if the caller object has own environment then we use its environment as_environment* env = fn.env; if (fn.this_ptr) { if (fn.this_ptr->get_environment()) { env = fn.this_ptr->get_environment(); } } // set 'this' as_object* this_ptr = env->get_target(); if (fn.this_ptr) { this_ptr = fn.this_ptr; if (this_ptr->m_this_ptr != NULL) { this_ptr = this_ptr->m_this_ptr.get_ptr(); } } // Function has been declared in moviclip ==> we should use its environment // At the same time 'this_ptr' may refers to another object // see testcase in .h file if (m_target != NULL) { character* ch = cast_to<character>(m_target.get_ptr()); if (ch) { if (ch->is_alive()) { env = m_target->get_environment(); } } } // Set up local stack frame, for parameters and locals. int local_stack_top = env->get_local_frame_top(); env->add_frame_barrier(); if (m_is_function2 == false) { // Conventional function. // Push the arguments onto the local frame. int args_to_pass = imin(fn.nargs, m_args.size()); for (int i = 0; i < args_to_pass; i++) { assert(m_args[i].m_register == 0); env->add_local(m_args[i].m_name, fn.arg(i)); } env->set_local("this", this_ptr); // Put 'super' in a local var. if (fn.this_ptr) { env->add_local("super", fn.this_ptr->get_proto()); } } else { // function2: most args go in registers; any others get pushed. // Create local registers. env->add_local_registers(m_local_register_count); // Handle the explicit args. int args_to_pass = imin(fn.nargs, m_args.size()); for (int i = 0; i < args_to_pass; i++) { if (m_args[i].m_register == 0) { // Conventional arg passing: create a local var. env->add_local(m_args[i].m_name, fn.arg(i)); } else { // Pass argument into a register. int reg = m_args[i].m_register; env->set_register(reg, fn.arg(i)); } } // Handle the implicit args. int current_reg = 1; if (m_function2_flags & 0x01) { // preload 'this' into a register. IF_VERBOSE_ACTION(log_msg("-------------- preload this=0x%X to register %d\n", this_ptr, current_reg)); env->set_register(current_reg, this_ptr); current_reg++; } if (m_function2_flags & 0x02) { // Don't put 'this' into a local var. } else { // Put 'this' in a local var. env->add_local("this", as_value(this_ptr)); } // Init arguments array, if it's going to be needed. smart_ptr<as_array> arg_array; if ((m_function2_flags & 0x04) || ! (m_function2_flags & 0x08)) { arg_array = new as_array(env->get_player()); as_value index_number; for (int i = 0; i < fn.nargs; i++) { index_number.set_int(i); arg_array->set_member(index_number.to_string(), fn.arg(i)); } } if (m_function2_flags & 0x04) { // preload 'arguments' into a register. env->set_register(current_reg, arg_array.get_ptr()); current_reg++; } if (m_function2_flags & 0x08) { // Don't put 'arguments' in a local var. } else { // Put 'arguments' in a local var. env->add_local("arguments", as_value(arg_array.get_ptr())); } if (m_function2_flags & 0x10) { // Put 'super' in a register. IF_VERBOSE_ACTION(log_msg("-------------- preload super=0x%X to register %d\n", fn.this_ptr->get_proto(), current_reg)); env->set_register(current_reg, fn.this_ptr->get_proto()); current_reg++; } if (m_function2_flags & 0x20) { // Don't put 'super' in a local var. } else { // Put 'super' in a local var. env->add_local("super", fn.this_ptr->get_proto()); } if (m_function2_flags & 0x40) { // Put '_root' in a register. env->set_register(current_reg, env->get_root()->get_root_movie()); current_reg++; } if (m_function2_flags & 0x80) { // Put '_parent' in a register. array<with_stack_entry> dummy; as_value parent = env->get_variable("_parent", dummy); IF_VERBOSE_ACTION(log_msg("-------------- preload _parent=0x%X to register %d\n", parent, current_reg)); env->set_register(current_reg, parent); current_reg++; } if (m_function2_flags & 0x100) { // Put '_global' in a register. IF_VERBOSE_ACTION(log_msg("-------------- preload _global=0x%X to register %d\n", get_global(), current_reg)); env->set_register(current_reg, get_global()); current_reg++; } } // keep stack size int stack_size = env->m_stack.size(); // printf("***on entry*** %d\n", stack_size); // Execute the actions. m_action_buffer.execute(env, m_start_pc, m_length, fn.result, m_with_stack, m_is_function2); // restore stack size // it should not be but it happens if (stack_size != env->m_stack.size()) { // log_error("s_function: on entry stack size (%d) != on exit stack size (%d)\n", // stack_size, env->m_stack.size()); env->m_stack.resize(stack_size); } // Clean up stack frame. env->set_local_frame_top(local_stack_top); if (m_is_function2) { // Clean up the local registers. env->drop_local_registers(m_local_register_count); } }
Atom RegExpObject::replace(Stringp subject, Stringp replacement) { UsesUTF8String utf8Subject(subject); UsesUTF8String utf8Replacement(replacement); int ovector[OVECTOR_SIZE]; int subjectLength = utf8Subject->length(); int lastIndex=0; StringBuffer resultBuffer(core()); const char *src = utf8Subject->c_str(); // get start/end index of all matches int matchCount; while (lastIndex <= subjectLength && (matchCount = pcre_exec((pcre*)m_pcreInst, NULL, src, subjectLength, lastIndex, PCRE_NO_UTF8_CHECK, ovector, OVECTOR_SIZE)) > 0) { int captureCount = matchCount-1; int matchIndex = ovector[0]; int matchLen = ovector[1]-ovector[0]; // copy in stuff leading up to match resultBuffer.write(src+lastIndex, matchIndex-lastIndex); const char *ptr = utf8Replacement->c_str(); while (*ptr) { if (*ptr == '$') { switch (*(ptr+1)) { case '$': resultBuffer << '$'; ptr += 2; break; case '&': resultBuffer.write(src+matchIndex, matchLen); ptr += 2; break; case '`': resultBuffer.write(src, matchIndex); ptr += 2; break; case '\'': resultBuffer << src+ovector[1]; ptr += 2; break; case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': { int i; if (*(ptr+2) >= '0' && *(ptr+2) <= '9') { int j = 10*(ptr[1]-'0')+(ptr[2]-'0'); if (j > captureCount) { // Gobbling up two digits would overflow the // capture count, so just use the one digit. i = ptr[1]-'0'; } else { i = j; } } else { i = ptr[1]-'0'; } if (i >= 1 && i <= captureCount) { resultBuffer.write(src+ovector[i*2], ovector[i*2+1]-ovector[i*2]); ptr += (i >= 10) ? 3 : 2; } else { resultBuffer << *ptr++; } } break; default: resultBuffer << *ptr++; break; } } else { resultBuffer << *ptr++; } } int newLastIndex = ovector[0] + (ovector[1] - ovector[0]); // prevents infinite looping in certain cases fixReplaceLastIndex(src, subjectLength, lastIndex, newLastIndex, resultBuffer); lastIndex = newLastIndex; if (!get_global()) { break; } } // copy in stuff after last match if (lastIndex < subjectLength) { resultBuffer.write(src+lastIndex, subjectLength-lastIndex); } return stringFromUTF8(resultBuffer.c_str(), resultBuffer.length()); }
Atom RegExpObject::replace(Stringp subject, ScriptObject* replaceFunction) { UsesUTF8String utf8Subject(subject); int ovector[OVECTOR_SIZE]; int subjectLength = utf8Subject->length(); int lastIndex=0; StringBuffer resultBuffer(core()); const char *src = utf8Subject->c_str(); // get start/end index of all matches int matchCount; while (lastIndex < subjectLength && (matchCount = pcre_exec((pcre*)m_pcreInst, NULL, src, subjectLength, lastIndex, PCRE_NO_UTF8_CHECK, ovector, OVECTOR_SIZE)) > 0) { int captureCount = matchCount-1; int matchIndex = ovector[0]; int matchLen = ovector[1]-ovector[0]; // copy in stuff leading up to match resultBuffer.write(src+lastIndex, matchIndex-lastIndex); // call the replace function Atom argv[NUM_MATCHES+4]; int argc = captureCount+3; argv[0] = undefinedAtom; // ECMA 15.5.4.11: Argument 1 is the substring that matched. argv[1] = core()->newString(src+matchIndex, matchLen)->atom(); // ECMA 15.5.4.11: The next m arguments are all of the captures in the // MatchResult for (int i=1; i<=captureCount; i++) { argv[i+1] = core()->newString(src+ovector[i*2], ovector[i*2+1]-ovector[i*2])->atom(); } // ECMA 15.5.4.11: Argument m+2 is the offset within string // where the match occurred argv[captureCount+2] = core()->uintToAtom(matchIndex); // ECMA 15.5.4.11: Argument m+3 is string argv[captureCount+3] = subject->atom(); resultBuffer << core()->string(toplevel()->op_call(replaceFunction->atom(), argc, argv)); int newLastIndex = ovector[0] + (ovector[1] - ovector[0]); // prevents infinite looping in certain cases fixReplaceLastIndex(src, subjectLength, lastIndex, newLastIndex, resultBuffer); lastIndex = newLastIndex; if (!get_global()) break; } // copy in stuff after last match if (lastIndex < subjectLength) { resultBuffer.write(src+lastIndex, subjectLength-lastIndex); } return stringFromUTF8(resultBuffer.c_str(), resultBuffer.length()); }
template<typename T> value_t get_global(T &&name) { return get_global(symbol_cast(std::forward<T>(name))); }
//_________________memserver____________________________ void memserver::set_allocator(block_allocator & al){ get_global() = & al; al.clean_garbage();//top object of al could have been marked unused in the meantime };
int get_global_2(void) { return get_global(); }
// interperate action script bytecode void as_3_function::execute ( array<as_value>& lregister, as_environment *env, as_value *result ) { // m_abc may be destroyed assert ( m_abc != NULL ); vm_stack &stack = *env; vm_stack &scope = env->m_scope; // some method have no body if ( m_code.size() == 0 ) { return; } int ip = 0; do { Uint8 opcode = m_code[ip++]; switch ( opcode ) { case 0x11: // iftrue { bool taken; //Follows ECMA-262 11.9.3 taken = stack.top ( 0 ).to_bool(); stack.drop ( 1 ); if ( taken ) { int offset = m_code[ip] | m_code[ip+1]<<8 | m_code[ip+2]<<16; ip += offset; } ip += 3; IF_VERBOSE_ACTION ( log_msg ( "EX: iftrue\t %s\n", taken? "taken": "not taken" ) ); } break; case 0x12: // iffalse { bool taken; //Follows ECMA-262 11.9.3 taken = !stack.top ( 0 ).to_bool(); stack.drop ( 1 ); if ( taken ) { int offset = m_code[ip] | m_code[ip+1]<<8 | m_code[ip+2]<<16; ip += offset; } ip += 3; IF_VERBOSE_ACTION ( log_msg ( "EX: iffalse\t %s\n", taken? "taken": "not taken" ) ); } break; case 0x14: // ifne { bool taken; //Follows ECMA-262 11.9.3 taken = !as_value::abstract_equality_comparison ( scope[ scope.size() - 2 ], scope[ scope.size() - 1 ] ); if ( taken ) { int offset = m_code[ip] | m_code[ip+1]<<8 | m_code[ip+2]<<16; ip += offset; } ip += 3; IF_VERBOSE_ACTION ( log_msg ( "EX: ifne\t %s\n", taken? "taken": "not taken" ) ); } break; case 0x1D: // popscope { scope.pop(); IF_VERBOSE_ACTION ( log_msg ( "EX: popscope\n" ) ); break; } case 0x20: // pushnull { as_value value; value.set_null(); stack.push ( value ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushnull\n" ) ); } break; case 0x24: // pushbyte { int byte_value; ip += read_vu30 ( byte_value, &m_code[ip] ); stack.push ( byte_value ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushbyte\t %d\n", byte_value ) ); break; } case 0x25: // pushshort { int val; ip += read_vu30 ( val, &m_code[ip] ); stack.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushshort\t %d\n", val ) ); break; } case 0x26: // pushtrue { stack.push ( true ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushtrue\n" ) ); } break; case 0x27: // pushfalse { stack.push ( false ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushfalse\n" ) ); } break; case 0x29: // pop the value from stack and discard it { stack.pop(); IF_VERBOSE_ACTION ( log_msg ( "EX: pop\n" ) ); break; } case 0x2A: // dup { IF_VERBOSE_ACTION ( log_msg ( "EX: dup %s\n", stack.top ( 0 ).to_xstring() ) ); stack.push ( stack.top ( 0 ) ); } break; case 0x2D: // pushint { int index; ip += read_vu30 ( index, &m_code[ip] ); int val = m_abc->get_integer ( index ); stack.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushint\t %d\n", val ) ); break; } case 0x2C: // pushstring { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *val = m_abc->get_string ( index ); stack.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushstring\t '%s'\n", val ) ); break; } case 0x2F: // pushdouble { int index; ip += read_vu30 ( index, &m_code[ip] ); double val = m_abc->get_double ( index ); stack.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushdouble\t %f\n", val ) ); break; } case 0x30: // pushscope { as_value val = stack.pop(); scope.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: pushscope\t %s\n", val.to_xstring() ) ); break; } case 0x46: // callproperty { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); int arg_count; ip += read_vu30 ( arg_count, &m_code[ip] ); as_environment env ( get_player() ); for ( int i = 0; i < arg_count; i++ ) { env.push ( stack.top ( i ) ); } stack.drop ( arg_count ); as_value result; if ( stack.top ( 0 ).is_object() ) { as_object *obj = stack.top ( 0 ).to_object(); as_value func, func2; result.set_undefined(); if ( obj && obj->get_member ( name, &func ) ) { if ( func.is_function() ) { result = call_method ( func, &env, obj, arg_count, env.get_top_index() ); } else if ( func.to_object()->get_member ( "__call__", &func2 ) ) { //todo patch scope result = call_method ( func2, &env, obj, arg_count, env.get_top_index() ); } } } else { as_value func; if ( stack.top ( 0 ).find_property ( name, &func ) ) { result = call_method ( func, &env, stack.top ( 0 ), arg_count, env.get_top_index() ); } } IF_VERBOSE_ACTION ( log_msg ( "EX: callproperty\t 0x%p.%s(args:%d), result %s\n", stack.top ( 0 ).to_xstring(), name, arg_count, result.to_xstring() ) ); stack.drop ( 1 ); stack.push ( result ); } break; case 0x47: // returnvoid { IF_VERBOSE_ACTION ( log_msg ( "EX: returnvoid\t\n" ) ); result->set_undefined(); return; } case 0x48: // returnvalue { IF_VERBOSE_ACTION ( log_msg ( "EX: returnvalue \t%s\n", stack.top ( 0 ).to_xstring() ) ); *result = stack.pop(); return; } case 0x49: // constructsuper { // stack: object, arg1, arg2, ..., argn int arg_count; ip += read_vu30 ( arg_count, &m_code[ip] ); as_environment env ( get_player() ); for ( int i = 0; i < arg_count; i++ ) { env.push ( stack.pop() ); } gc_ptr<as_object> obj = stack.pop().to_object(); // Assume we are in a constructor tu_string class_name = m_abc->get_class_from_constructor ( m_method ); tu_string super_class_name = m_abc->get_super_class ( class_name ); as_object *super = obj.get_ptr(); while ( super->get_proto() ) { super = super->get_proto(); } as_function *function = m_abc->get_class_constructor ( super_class_name ); if ( !function ) { as_value value; if ( get_player()->get_global()->get_member ( super_class_name, &value ) ) { function = cast_to<as_function> ( value.to_object() ); } } assert ( function ); as_object *proto = super->create_proto ( function ); UNUSED ( proto ); call_method ( function, &env, obj.get_ptr(), arg_count, 0 ); //stack.top(0) = obj.get_ptr(); IF_VERBOSE_ACTION ( log_msg ( "EX: constructsuper\t 0x%p(args:%d)\n", obj.get_ptr(), arg_count ) ); break; } case 0x4A: //constructprop // Stack ..., obj, [ns], [name], arg1,...,argn => ..., value { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); const char *name_space = m_abc->get_multiname_namespace ( index ); UNUSED ( name_space ); int arg_count; ip += read_vu30 ( arg_count, &m_code[ip] ); as_environment env ( get_player() ); for ( int i = 0; i < arg_count; i++ ) { env.push ( stack.top ( i ) ); } stack.drop ( arg_count ); as_object *obj = stack.pop().to_object(); as_value func, func2; gc_ptr<as_object> new_object; if ( obj && obj->get_member ( name, &func ) ) { instance_info *ii = m_abc->get_instance_info ( name ); new_object = new as_object ( get_player() ); //:TODO: create prototype .... ( move instanciate class from character -> as_object ) new_object->set_instance ( ii ); new_object->create_traits ( m_abc.get_ptr(), ii ); call_method ( m_abc->get_class_constructor ( name ), &env, new_object.get_ptr(), arg_count, 0 ); } IF_VERBOSE_ACTION ( log_msg ( "EX: constructprop\t 0x%p.%s(args:%d)\n", obj, name, arg_count ) ); stack.push ( new_object.get_ptr() ); } break; case 0x4F: // callpropvoid, Call a property, discarding the return value. // Stack: ..., obj, [ns], [name], arg1,...,argn => ... { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); int arg_count; ip += read_vu30 ( arg_count, &m_code[ip] ); as_environment env ( get_player() ); for ( int i = 0; i < arg_count; i++ ) { env.push ( stack.top ( i ) ); } stack.drop ( arg_count ); as_object *obj = stack.pop().to_object(); as_value func, func2; if ( obj && obj->get_member ( name, &func ) ) { if ( func.is_function() ) { call_method ( func, &env, obj, arg_count, env.get_top_index() ); } else if ( func.to_object()->get_member ( "__call__", &func2 ) ) { //todo patch scope call_method ( func2, &env, obj, arg_count, env.get_top_index() ); } } else { if ( !obj ) { log_msg ( "Error #1010: A term is undefined and has no properties (%s call)\n", name ); } else { log_msg ( "Error #1006: value is not a function (%s call)\n", name ); } } IF_VERBOSE_ACTION ( log_msg ( "EX: callpropvoid\t 0x%p.%s(args:%d)\n", obj, name, arg_count ) ); break; } case 0x56: //newarray { int arg_count; ip += read_vu30 ( arg_count, &m_code[ip] ); as_array *array = new as_array ( get_player() ); int offset = stack.size() - arg_count; for ( int arg_index = 0; arg_index < arg_count; ++arg_index ) { array->push ( stack[ offset + arg_index ] ); } stack.resize ( offset + 1 ); stack.top ( 0 ) = array; IF_VERBOSE_ACTION ( log_msg ( "EX: newarray\t arg_count:%i\n", arg_count ) ); } break; case 0x58: // newclass { // stack: ..., basetype => ..., newclass int class_index; ip += read_vu30 ( class_index, &m_code[ip] ); IF_VERBOSE_ACTION ( log_msg ( "EX: newclass\t class index:%i\n", class_index ) ); // as_object* basetype = stack.top(0).to_object(); gc_ptr<as_class> new_class = new as_class ( get_player() ); //new_class->set_proto(basetype); new_class->set_class ( m_abc->get_class_info ( class_index ) ); as_environment env ( get_player() ); call_method ( m_abc->get_class_function ( class_index ), &env, new_class.get_ptr(), 0, 0 ); stack.top ( 0 ).set_as_object ( new_class.get_ptr() ); break; } case 0x5D: // findpropstrict { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); // search property in scope as_object *obj = scope.find_property ( name ); //Search for a script entry to execute as_function *func = m_abc->get_script_function ( name ); if ( obj == NULL && func != NULL ) { get_global()->set_member ( name, new as_object ( get_player() ) ); as_environment env ( get_player() ); call_method ( func, &env, get_global(), 0, 0 ); obj = get_global(); } IF_VERBOSE_ACTION ( log_msg ( "EX: findpropstrict\t %s, obj=0x%p\n", name, obj ) ); stack.push ( obj ); break; } case 0x5E: // findproperty, Search the scope stack for a property { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); const char *name_space = m_abc->get_multiname_namespace ( index ); UNUSED ( name_space ); as_object *obj = scope.find_property ( name ); if ( obj ) { IF_VERBOSE_ACTION ( log_msg ( "EX: findproperty\t '%s', obj=0x%p\n", name, obj ) ); stack.push ( obj ); } else { IF_VERBOSE_ACTION ( log_msg ( "EX: findproperty\t '%s', obj=global\n", name ) ); stack.push ( get_global() ); } break; } case 0x60: // getlex, Find and get a property. { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); // search and get property in scope as_value val; scope.get_property ( name, &val ); if ( val.is_undefined() ) { as_function *func = m_abc->get_script_function ( name ); if ( func != NULL ) { gc_ptr<as_object> object = new as_object ( get_player() ); get_global()->set_member ( name, object.get() ); as_environment env ( get_player() ); call_method ( func, &env, get_global(), 0, 0 ); val.set_as_object ( object ); } } IF_VERBOSE_ACTION ( log_msg ( "EX: getlex\t %s, value=%s\n", name, val.to_xstring() ) ); stack.push ( val ); break; } case 0x61: // setproperty { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); IF_VERBOSE_ACTION ( log_msg ( "EX: setproperty\t %s.%s, value=%s\n", stack.top ( 1 ).to_xstring(), name, stack.top ( 0 ).to_xstring() ) ); as_object *object = stack.top ( 1 ).to_object(); if ( object ) { object->set_member ( name, stack.top ( 0 ) ); } stack.drop ( 2 ); } break; case 0x62: // getlocal { int index; ip += read_vu30 ( index, &m_code[ip] ); IF_VERBOSE_ACTION ( log_msg ( "EX: getlocal\t index=%i, value=%s\n", index, lregister[index].to_xstring() ) ); stack.push ( lregister[index] ); } break; case 0x63: // setlocal { int index; ip += read_vu30 ( index, &m_code[ip] ); IF_VERBOSE_ACTION ( log_msg ( "EX: setlocal\t index=%i, value=%s\n", index, stack.top ( 0 ).to_xstring() ) ); lregister[index] = stack.pop(); } break; case 0x65: // getscopeobject { int index = m_code[ip]; ++ip; assert ( index < scope.size() ); stack.push ( scope[index] ); IF_VERBOSE_ACTION ( log_msg ( "EX: getscopeobject\t index=%i, value=%s\n", index, stack.top ( 0 ).to_xstring() ) ); } break; case 0x66: // getproperty { int index; ip += read_vu30 ( index, &m_code[ip] ); tu_string name = get_multiname ( index, stack ); as_object *obj = stack.top ( 0 ).to_object(); if ( obj ) { obj->get_member ( name, &stack.top ( 0 ) ); } else { stack.top ( 0 ).set_undefined(); } IF_VERBOSE_ACTION ( log_msg ( "EX: getproperty\t %s, value=%s\n", name.c_str(), stack.top ( 0 ).to_xstring() ) ); break; } case 0x68: // initproperty, Initialize a property. { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *name = m_abc->get_multiname ( index ); as_value &val = stack.top ( 0 ); as_object *obj = stack.top ( 1 ).to_object(); if ( obj ) { obj->set_member ( name, val ); } IF_VERBOSE_ACTION ( log_msg ( "EX: initproperty\t 0x%p.%s=%s\n", obj, name, val.to_xstring() ) ); stack.drop ( 2 ); break; } case 0x73: //convert_i { stack.top ( 0 ).set_int ( stack.top ( 0 ).to_int() ); IF_VERBOSE_ACTION ( log_msg ( "EX: convert_i : %i \n", stack.top ( 0 ).to_int() ) ); } break; case 0x80: // coerce { int index; ip += read_vu30 ( index, &m_code[ip] ); const char *type_name = m_abc->get_multiname ( index ); // stack.push( stack.top( index ) ); IF_VERBOSE_ACTION ( log_msg ( "EX: coerce : %s todo\n", type_name ) ); } break; case 0x85: // coerce_s { stack.top ( 0 ).set_string ( stack.top ( 0 ).to_string() ); IF_VERBOSE_ACTION ( log_msg ( "EX: coerce_s : %s\n", stack.top ( 0 ).to_string() ) ); } break; case 0x96: // not { stack.top ( 0 ).set_bool ( !stack.top ( 0 ).to_bool() ); IF_VERBOSE_ACTION ( log_msg ( "EX: not\n" ) ); } break; case 0xA0: // Add two values { if ( stack.top ( 0 ).is_string() || stack.top ( 1 ).is_string() ) { tu_string str = stack.top ( 1 ).to_string(); str += stack.top ( 0 ).to_string(); stack.top ( 1 ).set_tu_string ( str ); } else { stack.top ( 1 ) += stack.top ( 0 ).to_number(); } stack.drop ( 1 ); break; } case 0xA2: // multiply { stack.top ( 1 ) = stack.top ( 1 ).to_number() * stack.top ( 0 ).to_number(); stack.drop ( 1 ); IF_VERBOSE_ACTION ( log_msg ( "EX: multiply\n" ) ); break; } case 0xAB: // equals { bool result = as_value::abstract_equality_comparison ( stack.top ( 1 ), stack.top ( 0 ) ); IF_VERBOSE_ACTION ( log_msg ( "EX: equals %s & %s : %s\n", stack.top ( 0 ).to_xstring(), stack.top ( 1 ).to_xstring(), result? "true":"false" ) ); stack.drop ( 1 ); stack.top ( 0 ).set_bool ( result ); } break; case 0xAD: //lessthan { as_value result = as_value::abstract_relational_comparison ( stack.top ( 1 ), stack.top ( 0 ) ); IF_VERBOSE_ACTION ( log_msg ( "EX: lessthan %s & %s : %s\n", stack.top ( 1 ).to_xstring(), stack.top ( 0 ).to_xstring(), result.to_string() ) ); stack.drop ( 1 ); stack.top ( 0 ) = result; } break; case 0xC2: // inclocal_i { int index; ip += read_vu30 ( index, &m_code[ip] ); as_value ® = lregister[ index ]; reg.set_int ( reg.to_int() + 1 ); IF_VERBOSE_ACTION ( log_msg ( "EX: inclocal_i %i\n", index ) ); } break; case 0xD0: // getlocal_0 case 0xD1: // getlocal_1 case 0xD2: // getlocal_2 case 0xD3: // getlocal_3 { as_value &val = lregister[opcode & 0x03]; stack.push ( val ); IF_VERBOSE_ACTION ( log_msg ( "EX: getlocal_%d\t %s\n", opcode & 0x03, val.to_xstring() ) ); break; } case 0xD4: // setlocal_0 case 0xD5: // setlocal_1 case 0xD6: // setlocal_2 case 0xD7: // setlocal_3 { lregister[opcode & 0x03] = stack.pop(); IF_VERBOSE_ACTION ( log_msg ( "EX: setlocal_%d\t %s\n", opcode & 0x03, lregister[opcode & 0x03].to_xstring() ) ); break; } default: log_msg ( "TODO opcode 0x%02X\n", opcode ); return; } } while ( ip < m_code.size() ); }
void memserver::error_allocate(big_size size_bytes){ get_global()->error_allocate(size_bytes); };
/* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ const struct map_t* text_group_get_all(void) { return &g_text_groups; } /* ------------------------------------------------------------------------- */ /* ------------------------------------------------------------------------- */ SERVICE(text_group_create_wrapper) { EXTRACT_ARGUMENT_PTR(0, file_name, const char*); EXTRACT_ARGUMENT(1, char_size, uint32_t, uint32_t); struct glob_t* g = get_global(service->plugin->game); RETURN(text_group_create(g, file_name, char_size), uint32_t); } /* ------------------------------------------------------------------------- */ SERVICE(text_group_destroy_wrapper) { EXTRACT_ARGUMENT(0, id, uint32_t, uint32_t); text_group_destroy(id); } /* ------------------------------------------------------------------------- */ SERVICE(text_group_load_character_set_wrapper)