void traceBuffer(char *label, unsigned char *buf, int bufLen) { AS3_Trace(AS3_String(label)); int len= 50; // chars per line int o, offset=0; for (o= 0; o<bufLen/len; o++, offset+=len) { debugBuf= malloc(sizeof(char)*(len*3)+1); int t; for (t= 0; t<len; t++) { debugBuf[t*3]= hex[(buf[t+offset]>>4)]; debugBuf[(t*3)+1]= hex[(buf[t+offset]&0xf)]; debugBuf[(t*3)+2]= ' '; } debugBuf[t*3]= 0; AS3_Trace(AS3_String(debugBuf)); free(debugBuf); } if (bufLen%len) { debugBuf= malloc(sizeof(char)*(len*3)+1); int t; for (t= 0; t<bufLen%len; t++) { debugBuf[t*3]= hex[(buf[t+offset]>>4)]; debugBuf[(t*3)+1]= hex[(buf[t+offset]&0xf)]; debugBuf[(t*3)+2]= ' '; } debugBuf[t*3]= 0; AS3_Trace(AS3_String(debugBuf)); free(debugBuf); }
/* * Given an ActionScript object, push it onto the Lua stack as a Lua native * type if a primitive class (String, Number, Boolean, int, null). * If object is not convertible to native Lua value, do not push anything * (and return 0). * * WARNING: It important that this function does not touch * non-primitive values (like Arrays). If this will be changed, * optional primitive autoconversion logic will break. */ int push_as3_to_lua_stack_if_convertible(lua_State * L, AS3_Val val) { LCALL(L, stack); #ifdef DO_SPAM SPAM(("push_as3_to_lua_stack_if_convertible(): begin: value, type")); AS3_Trace(val); AS3_Trace( AS3_Call( getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", val) ) ); #endif /* DO_SPAM */ if (AS3_InstanceOf(val, Number_class)) { lua_pushnumber(L, AS3_NumberValue(val)); } else if (AS3_InstanceOf(val, int_class)) { lua_pushinteger(L, AS3_IntValue(val)); } else if (AS3_InstanceOf(val, String_class)) { size_t length = 0; AS3_Malloced_Str str = get_string_bytes(val, &length); lua_pushlstring(L, str, length); free(str); } else if (AS3_InstanceOf(val, Boolean_class)) { lua_pushboolean(L, AS3_IntValue(val)); } else if (val == AS3_Undefined()) { lua_pushnil(L); } else if (is_null(val)) { lua_pushnil(L); } else { SPAM(("push_as3_to_lua_stack_if_convertible(): not convertible")); LRETURN(L, stack, 0); } SPAM(("push_as3_to_lua_stack_if_convertible(): end")); LRETURN(L, stack, 1); }
/* * Helpers */ int trace( const char *message ) { AS3_Val as3_message = AS3_String(message); AS3_Trace(as3_message); AS3_Release(as3_message); return 0; }
/* * Create an ActionScript value from the lua stack starting * at index start and ending at index end. If collapse_array == 1, * an empty return will be transformed into AS3_Undefined() and a * return of length 1 will just return the specific value. * Otherwise an array is returned. */ AS3_Val create_as3_value_from_lua_stack( lua_State * L, int start, int end, BOOL collapse_array ) { /* WARNING: Panic alert! Use L*_FN checkers here! */ LCALL(L, stack); AS3_Val ret; SPAM(("create_as3_value_from_lua_stack(): begin")); if (collapse_array == TRUE && start > end) { ret = AS3_Null(); } else if (collapse_array == TRUE && start == end) { ret = get_as3_value_from_lua_stack(L, start); } else { int i; ret = AS3_Array(""); for (i = start; i <= end; ++i) { AS3_Val value; AS3_Val r; /*SPAM(("create_as3_value_from_lua_stack() + 1 begin"));*/ value = get_as3_value_from_lua_stack(L, i); r = AS3_CallTS("push", ret, "AS3ValType", value); SAFE_RELEASE(r); /* Ignoring result */ SAFE_RELEASE(value); /*SPAM(("create_as3_value_from_lua_stack() + 1 end"));*/ } } #ifdef DO_SPAM SPAM(("create_as3_value_from_lua_stack(): end")); AS3_Trace( AS3_Call( getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", ret) ) ); #endif /* DO_SPAM */ LCHECK_FN(L, stack, 0, fatal_error); return ret; }
void scws_send_text_AS3() { char *text = NULL; AS3_MallocString(text, inputString); scws_send_text(s, text, strlen(text)); AS3_DeclareVar(myString, String); //char *result; //result[0] = '\0'; // ensures the memory is an empty string char result[5000]={"0"}; char temp[1000]={'\0'}; printf("%s",result); while (res = cur = scws_get_result(s)) { while (cur != NULL) { printf("WORD: %.*s/%s (IDF = %4.2f)\n", cur->len, text+cur->off, cur->attr, cur->idf); //if((result = malloc(strlen(result)+ cur->len +1)) != NULL){ //if((result = (char*) realloc(strlen(result)+ (cur->len) +1)) != NULL) strncpy(temp, text+cur->off, cur->len); temp[(cur->len)+1]='\0'; strcat(result, temp); strcat(result, ' '); strcat(result, '\0'); //strncpy(new_str,str2); //} else { //printf("malloc failed!\n"); // exit? //} cur = cur->next; } scws_free_result(res); } strcat(result, '\0'); printf("%s",result); AS3_CopyCStringToVar(myString, result, strlen(result)); scws_free(s); //scws_free(result); AS3_Trace(myString); AS3_Return("212"); }
/* * Take the Lua stack item at index i and convert it into an * ActionScript value. */ AS3_Val get_as3_value_from_lua_stack_type(lua_State * L, int i, int type) { /* WARNING: Panic alert! Use L*_FN checkers here! */ LCALL(L, stack); AS3_Val value; switch (type) { case LUA_TSTRING: /* strings */ { size_t length = 0; const char * str = lua_tolstring(L, i, &length); if (str == NULL) /* NOTE: This is unreachable. Assert instead */ { length = 6; str = "(null)"; } /* NOTE: Alchemy .5a truncates embedded zeroes in string * regardless to the passed length */ value = AS3_StringN(str, length); } break; case LUA_TBOOLEAN: /* booleans */ value = lua_toboolean(L, i) ? AS3_True() : AS3_False(); break; case LUA_TNUMBER: /* numbers */ value = AS3_Number(lua_tonumber(L, i)); break; case LUA_TNONE: /* fall through */ case LUA_TNIL: /* nil */ value = AS3_Null(); break; case LUA_TUSERDATA: /* userdata */ { void * userdata = lua_touserdata(L, i); lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_METATABLE); if (userdata == NULL || !lua_getmetatable(L, i)) { lua_pop(L, 1); /* Pop AS3LUA_METATABLE */ value = as3_value_from_foreign_userdata(L, i); } else if (!lua_rawequal(L, -2, -1)) { lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */ value = as3_value_from_foreign_userdata(L, i); } else { lua_pop(L, 2); /* Pop AS3LUA_METATABLE and userdata metatable */ AS3LuaUserData * userdata = (AS3LuaUserData *)lua_touserdata(L, i); value = userdata->value; /* * We just created one more reference to the AS3 value, * as it still lives inside Lua. * (And will probably be collected by GC.) */ AS3_Acquire(value); } } break; case LUA_TFUNCTION: /* function */ value = setup_callback(L, i); break; case LUA_TLIGHTUSERDATA: /* TODO: blackbox this type */ case LUA_TTABLE: /* TODO: deal with this type */ case LUA_TTHREAD: /* TODO: blackbox this type */ value = AS3_String(lua_typename(L, type)); break; default: /* unreachable */ fatal_error("unknown Lua type"); break; } #ifdef DO_SPAM SPAM(("get_as3_value_from_lua_stack(): end")); AS3_Trace( AS3_Call( getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", value) ) ); #endif /* DO_SPAM */ LCHECK_FN(L, stack, 0, fatal_error); return value; }
/* * Function used as a callback for all Lua functions passed through * get_as3_value_from_lua_stack() */ AS3_Val as3_lua_callback(void * data, AS3_Val args) { /* WARNING: Panic alert! Use L*_FN checkers here! */ SPAM(("as3_lua_callback(): begin")); AS3_Val res; LuaFunctionCallbackData * func_data = (LuaFunctionCallbackData *) data; int nargs = 0; int status = 0; int results_base = 0; lua_State * L = func_data->L; if (L == NULL) { /* TODO: Should we crash here? fatal_error("state expired"); / * Does not return * / */ sztrace("as3_lua_callback: state expired"); return AS3_Undefined(); } { /* A new scope for LCALL to work (C89 conformance) */ LCALL(L, stack); /* TODO: Cache that with lua_ref, it is faster */ lua_getfield(L, LUA_REGISTRYINDEX, AS3LUA_CALLBACKS); /* TODO: Assert we have a table here */ lua_rawgeti(L, -1, func_data->ref); /* push stored function */ if (lua_istable(L, -1) == 0) /* Probably nil */ { lua_pop(L, 1); /* Pop bad callback table */ LCHECK_FN(L, stack, 0, fatal_error); fatal_error("function callback not found"); /* Does not return */ } lua_rawgeti(L, -1, AS3LUA_CBFNINDEX); /* push stored callback function */ #ifdef DO_SPAM { SPAM(("as3_lua_callback(): AS3 arguments")); AS3_Val a = AS3_CallS("join", args, AS3_Undefined()); AS3_Trace(a); SAFE_RELEASE(a); } #endif /* DO_SPAM */ /* TODO: Assert we have Lua function (or other callable object) on the top of the stack */ LCHECK_FN(L, stack, 2 + 1, fatal_error); nargs = push_as3_array_to_lua_stack(L, args); /* push arguments */ #ifdef DO_SPAM /* TODO: Remove */ lua_pushcfunction(L, as3_trace); dump_lua_stack(L, LBASE(L, stack) + 2 + 1); lua_pushliteral(L, "ARGUMENTS"); lua_pushnumber(L, nargs); lua_call(L, 3, 0); #endif /* DO_SPAM */ LCHECK_FN(L, stack, 2 + 1 + nargs, fatal_error); results_base = LBASE(L, stack) + 2; status = do_pcall_with_traceback(L, nargs, LUA_MULTRET); if (status != 0) { const char * msg = NULL; LCHECK_FN(L, stack, 2 + 1, fatal_error); /* Tables and error message */ lua_remove(L, -2); /* Remove AS3LUA_CALLBACKS table */ lua_remove(L, -2); /* Remove holder table */ LCHECK_FN(L, stack, 1, fatal_error); /* Only error message */ /* Error message is on stack */ /* NOTE: It is not necessary string! If we want to preserve its type, see lua_DoString. */ if (lua_tostring(L, -1) == NULL) { lua_pop(L, 1); lua_pushliteral(L, "(non-string)"); } LCHECK_FN(L, stack, 1, fatal_error); lua_pushliteral(L, "Error in Lua callback:\n"); lua_insert(L, -2); LCHECK_FN(L, stack, 2, fatal_error); lua_concat(L, 2); LCHECK_FN(L, stack, 1, fatal_error); sztrace((char *)lua_tostring(L, -1)); /* TODO: ?! */ /* lua_error(L); */ msg = lua_tostring(L, -1); lua_pop(L, 1); /* fatal_error(msg); / * Does not return * / */ } /* Process results */ #ifdef DO_SPAM /* TODO: Remove */ /* lua_pushcfunction(L, as3_trace); lua_pushliteral(L, "STACK"); dump_lua_stack(L, results_base); lua_call(L, 2, 0); */ #endif /* DO_SPAM */ res = create_as3_value_from_lua_stack(L, results_base + 1, LTOP(L, stack), TRUE); #ifdef DO_SPAM SPAM(("as3_lua_callback() result type")); AS3_Trace(AS3_Call(getQualifiedClassName_method, NULL, AS3_Array("AS3ValType", res))); #endif /* DO_SPAM */ lua_settop(L, LBASE(L, stack)); /* Cleanup results and two holder tables */ SPAM(("as3_lua_callback(): end")); return res; } /* Unreachable */ }