/* Two arguments version to avoid string concatenation */ void fatal_error2(const char * msg1, const char * msg2) { sztrace("Lua Alchemy fatal error"); sztrace((char *)msg1); sztrace((char *)msg2); exit(-1); }
/* * TODO: Do not assume that as3 table is avalable in global environment. * Store a reference to it in the registry instead. */ static BOOL set_async_state(lua_State * L, BOOL is_async) { /* WARNING: Panic alert! Use L*_FN checkers here! */ BOOL old_async = FALSE; lua_getglobal(L, "as3"); if (!lua_istable(L, -1)) { /* TODO: Hack. This should not be required */ lua_pop(L, 1); sztrace("as3 global table not available"); return FALSE; } lua_getfield(L, -1, "is_async"); old_async = lua_toboolean(L, -1); lua_pop(L, 1); /* old flag value */ lua_pushboolean(L, is_async); lua_setfield(L, -2, "is_async"); lua_pop(L, 1); /* as3 table */ return old_async; }
//Method exposed to ActionScript //Takes a String and echos it static AS3_Val echo(void* self, AS3_Val args) { AS3_Val byteArray; AS3_ArrayValue( args, "AS3ValType", &byteArray); AS3_Val length = AS3_GetS(byteArray, "length"); sztrace("length getted!"); int len = AS3_IntValue(length); //if(len>0) return length; char *data=NULL; data=malloc(len); memset(data,0,len); int fileLen = AS3_ByteArray_readBytes(data,byteArray, len); char *out= NULL; out = malloc(fileLen); memset(out,0,fileLen); ByteArray * bytearray = ByteArray_new(fileLen); bytearray->data = data; PdfFile_free(PdfFile_parse(bytearray,out)); ByteArray_free(bytearray); return AS3_String(out); }
BOOL get_async_state(lua_State * L) { /* WARNING: Panic alert! Use L*_FN checkers here! */ BOOL result = FALSE; lua_getglobal(L, "as3"); if (!lua_istable(L, -1)) { /* * TODO: Hack. This should not be required, see comment for set_async_state() */ lua_pop(L, 1); sztrace("as3 global table not available"); return FALSE; } lua_getfield(L, -1, "is_async"); result = lua_toboolean(L, -1); lua_pop(L, 2); /* flag value, as3 table */ return result; }
/* * Create an empty RGB surface of the appropriate depth */ SDL_Surface * SDL_CreateRGBSurface (Uint32 flags, int width, int height, int depth, Uint32 Rmask, Uint32 Gmask, Uint32 Bmask, Uint32 Amask) { sztrace("SDL_CreateRGBSurface"); SDL_VideoDevice *video = current_video; SDL_VideoDevice *this = current_video; SDL_Surface *screen; SDL_Surface *surface; /* Make sure the size requested doesn't overflow our datatypes */ /* Next time I write a library like SDL, I'll use int for size. :) */ if ( width >= 16384 || height >= 65536 ) { SDL_SetError("Width or height is too large"); return(NULL); } /* Check to see if we desire the surface in video memory */ if ( video ) { screen = SDL_PublicSurface; } else { screen = NULL; } if ( screen && ((screen->flags&SDL_HWSURFACE) == SDL_HWSURFACE) ) { if ( (flags&(SDL_SRCCOLORKEY|SDL_SRCALPHA)) != 0 ) { flags |= SDL_HWSURFACE; } if ( (flags & SDL_SRCCOLORKEY) == SDL_SRCCOLORKEY ) { if ( ! current_video->info.blit_hw_CC ) { flags &= ~SDL_HWSURFACE; } } if ( (flags & SDL_SRCALPHA) == SDL_SRCALPHA ) { if ( ! current_video->info.blit_hw_A ) { flags &= ~SDL_HWSURFACE; } } } else { flags &= ~SDL_HWSURFACE; } /* Allocate the surface */ surface = (SDL_Surface *)SDL_malloc(sizeof(*surface)); if ( surface == NULL ) { SDL_OutOfMemory(); return(NULL); } surface->flags = SDL_SWSURFACE; if ( (flags & SDL_HWSURFACE) == SDL_HWSURFACE ) { if ((Amask) && (video->displayformatalphapixel)) { depth = video->displayformatalphapixel->BitsPerPixel; Rmask = video->displayformatalphapixel->Rmask; Gmask = video->displayformatalphapixel->Gmask; Bmask = video->displayformatalphapixel->Bmask; Amask = video->displayformatalphapixel->Amask; } else { depth = screen->format->BitsPerPixel; Rmask = screen->format->Rmask; Gmask = screen->format->Gmask; Bmask = screen->format->Bmask; Amask = screen->format->Amask; } } surface->format = SDL_AllocFormat(depth, Rmask, Gmask, Bmask, Amask); if ( surface->format == NULL ) { SDL_free(surface); return(NULL); } if ( Amask ) { surface->flags |= SDL_SRCALPHA; } surface->w = width; surface->h = height; surface->pitch = SDL_CalculatePitch(surface); surface->pixels = NULL; surface->offset = 0; surface->hwdata = NULL; surface->locked = 0; surface->map = NULL; surface->unused1 = 0; SDL_SetClipRect(surface, NULL); SDL_FormatChanged(surface); /* Get the pixels */ if ( ((flags&SDL_HWSURFACE) == SDL_SWSURFACE) || (video->AllocHWSurface(this, surface) < 0) ) { if ( surface->w && surface->h ) { surface->pixels = SDL_malloc(surface->h*surface->pitch); if ( surface->pixels == NULL ) { SDL_FreeSurface(surface); SDL_OutOfMemory(); return(NULL); } /* This is important for bitmaps */ SDL_memset(surface->pixels, 0, surface->h*surface->pitch); } } /* Allocate an empty mapping */ surface->map = SDL_AllocBlitMap(); if ( surface->map == NULL ) { SDL_FreeSurface(surface); return(NULL); } /* The surface is ready to go */ surface->refcount = 1; #ifdef CHECK_LEAKS ++surfaces_allocated; #endif return(surface); }
/* * 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 */ }