/* * Get an actionscript class with the given namespace and class name * in the format: package (may be NULL), ClassName */ AS3_Val get_class2(const char * as_namespace_path, const char * as_class_path) { AS3_Val as_namespace; AS3_Val as_class; AS3_Val ret; /* TODO might want to store classes in a table to save loading again */ if (as_namespace_path) { as_namespace = AS3_String(as_namespace_path); } else { as_namespace = AS3_Undefined(); } as_class = AS3_String(as_class_path); ret = AS3_NSGet(as_namespace, as_class); /* TODO check for failure getting class */ AS3_Release(as_namespace); AS3_Release(as_class); return ret; }
/* * Get an actionscript class with the given namespace and class name * in the format: package::ClassName */ AS3_Val get_class(const char * as_namespaceclass_path) { /* TODO: Merge with get_class2()? */ AS3_Val as_namespace; AS3_Val as_class; AS3_Val ret; char * class_ptr = NULL; /* TODO might want to store classes in a table to save loading again */ class_ptr = strstr(as_namespaceclass_path, "::"); if (class_ptr > as_namespaceclass_path) { as_namespace = AS3_StringN( as_namespaceclass_path, (class_ptr - as_namespaceclass_path) / sizeof(char) ); as_class = AS3_String(class_ptr + 2); } else { as_namespace = AS3_Undefined(); as_class = AS3_String(as_namespaceclass_path); } ret = AS3_NSGet(as_namespace, as_class); /* TODO check for failure getting class */ AS3_Release(as_namespace); AS3_Release(as_class); return ret; }
int release_callback(lua_State * L) { SPAM(("release_callback() : begin")); LuaFunctionCallbackData ** pUserdata = (LuaFunctionCallbackData **)luaL_checkudata( L, 1, AS3LUA_CALLBACKMT ); if (pUserdata == NULL) { SPAM(("release_callback() : bad userdata")); return 0; } LuaFunctionCallbackData * pCallback = *pUserdata; if (pCallback == NULL) { SPAM(("release_callback() : bad callback")); return 0; } if (pCallback->L != NULL) { if (pCallback->as3Function != AS3_Undefined()) { SPAM(("release_callback() : before release")); SAFE_RELEASE(pCallback->as3Function); SPAM(("release_callback() : after release")); pCallback->as3Function = AS3_Undefined(); } pCallback->L = NULL; } *pUserdata = NULL; SPAM(("release_callback() : end")); return 0; }
/* * 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); }
static AS3_Val initparam(void* self, AS3_Val args) { AS3_Val tmp = AS3_Undefined(); int il, len; for (il=0; il < sizeof(models)/sizeof(LAYER); ++il) { tmp = AS3_Get(args, AS3_Int(2*il)); len = AS3_IntValue(AS3_GetS(tmp, "length")); models[il].w = (float*) malloc(len); AS3_ByteArray_readBytes(models[il].w, tmp, len); tmp = AS3_Get(args, AS3_Int(2*il+1)); len = AS3_IntValue(AS3_GetS(tmp, "length")); models[il].b = (float*) malloc(len); AS3_ByteArray_readBytes(models[il].b, tmp, len); } data = (float*) malloc(BUF_SIZE); o = (float*) malloc(BUF_SIZE); return AS3_Int(0); }
AS3_Val call(void* thiz, AS3_Val args) { AS3_Val is; AS3_Val os; uint8_t* bufIn; uint8_t* bufOut; int i = 0; int j = 0; uint32_t color; uint32_t* bufDst; int w = 0; int h = 0; int len; int type; struct { uint16_t w; uint16_t h; } size; AS3_ArrayValue(args, "AS3ValType,IntType,IntType,AS3ValType", &is, &len, &type, &os); for(;;) { bufIn = malloc(len); if(!bufIn) break; AS3_ByteArray_readBytes(bufIn, is, len); bufOut = WebPDecodeRGB(bufIn, len, &w, &h); free(bufIn); if(!bufOut || w <= 0 || h <= 0) break; bufDst = (uint32_t*)malloc(w * h * 4); if(!bufDst) break; len = w * h * 3; while(i < len) { color = *(uint32_t*)(bufOut + i); color = (color << 8) | 0x000000FF; bufDst[j++] = color; i += 3; } free(bufOut); size.w = w; size.h = h; AS3_ByteArray_writeBytes(os, &size, sizeof(size)); AS3_ByteArray_writeBytes(os, bufDst, w * h * 4); free(bufDst); break; } return AS3_Undefined(); }
void errorToFlash(const char* message) { AS3_Val trace = AS3_NSGetS(NULL, "trace"); AS3_Val params = AS3_Array("StrType", message); AS3_Release(AS3_Call(trace, AS3_Undefined(), params)); }
/* * 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 */ }
static AS3_Val prediction(void* self, AS3_Val args) { AS3_Val in_arr = AS3_Undefined(), out_arr = AS3_Array(0); float *tmp, d, e; int i,j,k,l, n,x,y, newx,newy, il, dx,dy; LAYER *pL; AS3_ArrayValue( args, "AS3ValType", &in_arr ); for(i=0; i < 1024; ++i) data[i] = AS3_IntValue(AS3_Get(in_arr, AS3_Int(4*i+1))) /255.0; n = 1; x = 32; y = 32; #define DATA(l,j,i) data[((l)*y + (j))*x + (i)] #define O(k,dy,dx) o[((k)*newy + (dy))*newx + (dx)] #define W(k,l,j,i) pL->w[(((k)*n + (l))*pL->y + (j))*pL->x + (i)] for (il=0; il < nl; ++il) { flyield(); pL = L+il; newx = x+1-pL->x; newy = y+1-pL->y; for (dx=0; dx < newx; ++dx) for (dy=0; dy < newy; ++dy) for (k=0; k < pL->n; ++k) { d = pL->b[k]; for (l=0; l < n; ++l) for(j=0; j < pL->y; ++j) for(i=0; i < pL->x; ++i) d += DATA(l,j+dy,i+dx)*W(k,l,j,i); O(k,dy,dx) = d; } if(pL->maxpool) { for (k=0; k < pL->n; ++k) for (dx=0; dx < newx; dx+=2) for (dy=0; dy < newy; dy+=2) { d=O(k,dy,dx); e=O(k,dy,dx+1); if(e>d) d=e; e=O(k,dy+1,dx); if(e>d) d=e; e=O(k,dy+1,dx+1); if(e>d) d=e; O(k,dy/2,dx/2)=d; } newx /= 2; newy /= 2; } for (dx=0; dx < newx; ++dx) for (dy=0; dy < newy; ++dy) { e = 0; for (k=0; k < pL->n; ++k) { d = O(k,dy,dx); if(pL->nonlin==1) d=1.0/(1.0 + exp(-d)); else if(pL->nonlin==2) d=tanh(d); else if(pL->nonlin==3) { d=exp(d); e += d; } O(k,dy,dx) = d; } if(pL->nonlin==3 && e) for (k=0; k < pL->n; ++k) O(k,dy,dx) /= e; } tmp = data; data = o; o = tmp; x = newx; y = newy; n = pL->n; } for(i=0; i < n*x*y; ++i) AS3_Set(out_arr, AS3_Int(i), AS3_Number(data[i])); return out_arr; }