Ejemplo n.º 1
1
LUALIB_API lua_State *luaL_newstate (void) {
  lua_State *L = lua_newstate(l_alloc, NULL);
  lua_setallocf(L, l_alloc, L); /* allocator need lua_State. */
  if (L) lua_atpanic(L, &panic);
  return L;
}
Ejemplo n.º 2
0
LUALIB_API lua_State *luaL_newstate (void) {
  lua_State *L = lua_newstate(l_alloc, NULL);
  if (L) lua_atpanic(L, &panic);
  return L;
}
Ejemplo n.º 3
0
static int tek_lib_exec_run(lua_State *L)
{
	struct LuaExecTask *lexec = tek_lib_exec_check(L);
	struct TExecBase *TExecBase = lexec->exec;
	struct LuaExecChild *ctx;
	const char *fname = TNULL;
	const char *chunk = TNULL;
	const char *taskname = TNULL;
	size_t extralen = 0;
	struct THook hook;
	TTAGITEM tags[2];
	int nremove = 1;
	TBOOL abort = TTRUE;
	
	for (;;)
	{
		if (lua_istable(L, 1))
		{
			lua_getfield(L, 1, "abort");
			if (lua_isboolean(L, -1))
				abort = lua_toboolean(L, -1);
			lua_pop(L, 1);
			lua_getfield(L, 1, "taskname");
			taskname = lua_tostring(L, -1);
			nremove = 2;
			lua_getfield(L, 1, "func");
			if (!lua_isnoneornil(L, -1))
				break;
			lua_pop(L, 1);
			lua_getfield(L, 1, "filename");
			if (!lua_isnoneornil(L, -1))
				break;
			lua_pop(L, 1);
			lua_getfield(L, 1, "chunk");
			if (!lua_isnoneornil(L, -1))
			{
				chunk = luaL_checklstring(L, -1, &extralen);
				break;
			}
			luaL_error(L, "required argument missing");
		}
		lua_pushvalue(L, 1);
		break;
	}
	
	if (!chunk)
	{
		if (lua_type(L, -1) == LUA_TSTRING)
			fname = luaL_checklstring(L, -1, &extralen);
		else if (lua_isfunction(L, -1) && !lua_iscfunction(L, -1))
			chunk = tek_lib_exec_dump(L, &extralen);
		else
			luaL_error(L, "not a Lua function, filename or table");
	}
	
	ctx = lua_newuserdata(L, sizeof(struct LuaExecChild) + extralen + 1);
	memset(ctx, 0, sizeof *ctx);
	ctx->exec = lexec->exec;
	ctx->parent = lexec;
	ctx->taskname = tek_lib_exec_taskname(ctx->atomname, taskname);
	ctx->abort = abort;
	
	if (fname)
	{
		ctx->fname = (char *) (ctx + 1);
		strcpy(ctx->fname, (char *) fname);
	}
	else if (chunk)
	{
		memcpy(ctx + 1, chunk, extralen);
		ctx->chunklen = extralen;
	}
	
	/* remove arguments under userdata */
	while (nremove--)
		lua_remove(L, -2);

	ctx->numargs = lua_gettop(L) - 2;
	
	/* push arg[0] on the stack, will be extraarg */
	lua_getglobal(L, "arg");
	if (lua_istable(L, -1))
	{
		lua_rawgeti(L, -1, 0);
		lua_remove(L, -2);
	}
	if (lua_type(L, -1) != LUA_TSTRING)
	{
		lua_pop(L, 1);
		lua_pushnil(L);
	}
	ctx->args = tek_lib_exec_getargs(L, TExecBase, 2, ctx->numargs++, 1);
	lua_pop(L, 1);
	
	ctx->L = lua_newstate(tek_lib_exec_allocf, TExecBase);
	if (ctx->L == TNULL)
	{
		tek_lib_exec_freeargs(TExecBase, ctx->args, ctx->numargs);
		luaL_error(L, "cannot create interpreter");
	}
	
	tags[0].tti_Tag = TTask_UserData;
	tags[0].tti_Value = (TTAG) ctx;
	tags[1].tti_Tag = TTAG_DONE;
	TInitHook(&hook, tek_lib_exec_run_dispatch, ctx);
	ctx->task = TCreateTask(&hook, tags);
	if (ctx->task == TNULL)
	{
		tek_lib_exec_freectxargs(ctx);
		lua_pop(L, 1);
		lua_pushnil(L);
		return 1;
	}
	
	lua_getfield(L, LUA_REGISTRYINDEX, TEK_LIB_TASK_CLASSNAME);
	lua_setmetatable(L, -2);
	lua_pushvalue(L, -1);
	ctx->ref = luaL_ref(L, lua_upvalueindex(2));
	if (ctx->abort)
		tek_lib_exec_register_task_hook(L, TExecBase);
	return 1;
}
Ejemplo n.º 4
0
lua_State* clua_newstate(void* goallocf)
{
	return lua_newstate(&allocwrapper,goallocf);
}
Ejemplo n.º 5
0
LUALIB_API lua_State *luaL_newstate(void)
{
    lua_State *L = lua_newstate(mem_alloc, NULL);
    if (L) G(L)->panic = panic;
    return L;
}
Ejemplo n.º 6
0
	}

	efree(lua_obj);
}
/* }}} */

/** {{{ static zend_object_value php_lua_create_object(zend_class_entry *ce TSRMLS_DC)
 *
 * the create object handler for lua
 */
static zend_object_value php_lua_create_object(zend_class_entry *ce TSRMLS_DC) {
	zend_object_value obj 	 = {0};
	php_lua_object 	*lua_obj = NULL;
	lua_State 		*L	 	 = NULL;

	L = lua_newstate(php_lua_alloc_function, NULL);

	lua_atpanic(L, php_lua_atpanic);

	lua_obj = emalloc(sizeof(php_lua_object));

	if (!lua_obj) {
		php_error_docref(NULL TSRMLS_CC, E_ERROR, "alloc memory for lua object failed");
	}

	lua_obj->L = L;
	zend_object_std_init(&(lua_obj->obj), ce TSRMLS_CC);

#if (PHP_MAJOR_VERSION == 5) && (PHP_MINOR_VERSION < 4)
	zend_hash_copy(lua_obj->obj.properties, &ce->default_properties,
#if (PHP_MINOR_VERSION < 4)
Ejemplo n.º 7
0
void LSLuaState::open()
{
    assert(!L);

    L = lua_newstate(lsLuaAlloc, NULL);
    //L = lua_open();
    toLuaState.insert(L, this);

    luaopen_base(L);
    luaopen_table(L);
    luaopen_string(L);
    luaopen_math(L);
    luaL_openlibs(L);

    // TODO: turn this back on when it doesn't fail on the testWhile unit test
    // update luajit and test again
    luaJIT_setmode(L, 0, LUAJIT_MODE_ENGINE | LUAJIT_MODE_OFF);

    // open the lua debug library
    luaopen_debug(L);

    // open socket library
    luaopen_socket_core(L);

    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXCLASSES);

    lua_newtable(L);
    lua_setglobal(L, "__ls_nativeclasses");

    lua_pushcfunction(L, traceback);
    lua_setglobal(L, "__ls_traceback");
    _tracemessage[0] = 0;

    // entry -> version
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDVERSION);

    // entry -> native user data
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDUSERDATA);

    // native user data -> script instance
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMANAGEDNATIVESCRIPT);

    // native delegate table
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXNATIVEDELEGATES);

    // interned field name lookup
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMEMBERINFONAME);

    // typeid -> type*
    lua_newtable(L);
    lua_rawseti(L, LUA_GLOBALSINDEX, LSASSEMBLYLOOKUP);

    // lua/luacfunction -> MethodBase* lookups
    lua_newtable(L);

    // weak key metatable
    lua_newtable(L);
    lua_pushstring(L, "k");
    lua_setfield(L, -2, "__mode");
    lua_setmetatable(L, -2);

    lua_rawseti(L, LUA_GLOBALSINDEX, LSINDEXMETHODLOOKUP);

    lsr_instanceregister(L);

    NativeInterface::registerNativeTypes(L);
}
Ejemplo n.º 8
0
lua_State * CreateLuaState(void *pOpaque) {
	lua_State *pResult = lua_newstate(_lua_alloc, pOpaque);
	luaL_openlibs(pResult);
	return pResult;
}
Ejemplo n.º 9
0
int lua_sandbox_init(lua_sandbox* lsb, const char* data_file)
{
    if (lsb == NULL || lsb->m_lua != NULL) {
        return 0;
    }
    if (lsb->m_lua_file == NULL) {
        snprintf(lsb->m_error_message, ERROR_SIZE, "no Lua script provided");
        sandbox_terminate(lsb);
        return 1;
    }
    lsb->m_lua = lua_newstate(memory_manager, lsb);
    if (lsb->m_lua == NULL) {
        snprintf(lsb->m_error_message, ERROR_SIZE, "out of memory");
        sandbox_terminate(lsb);
        return 2;
    }

    load_library(lsb->m_lua, "", luaopen_base, disable_base_functions);
    lua_pop(lsb->m_lua, 1);
    load_library(lsb->m_lua, LUA_MATHLIBNAME, luaopen_math, disable_none);
    lua_pop(lsb->m_lua, 1);
    load_library(lsb->m_lua, LUA_OSLIBNAME, luaopen_os, disable_os_functions);
    lua_pop(lsb->m_lua, 1);
    load_library(lsb->m_lua, LUA_STRLIBNAME, luaopen_string, disable_none);
    lua_pop(lsb->m_lua, 1);
    load_library(lsb->m_lua, LUA_TABLIBNAME, luaopen_table, disable_none);
    lua_pop(lsb->m_lua, 1);
    load_library(lsb->m_lua, heka_circular_buffer_table, 
                 luaopen_circular_buffer, disable_none);
    lua_pop(lsb->m_lua, 1);

    lua_pushcfunction(lsb->m_lua, &require_library);
    lua_setglobal(lsb->m_lua, "require");

    lua_pushlightuserdata(lsb->m_lua, (void*)lsb);
    lua_pushcclosure(lsb->m_lua, &read_config, 1);
    lua_setglobal(lsb->m_lua, "read_config");

    lua_pushlightuserdata(lsb->m_lua, (void*)lsb);
    lua_pushcclosure(lsb->m_lua, &read_message, 1);
    lua_setglobal(lsb->m_lua, "read_message");

    lua_pushlightuserdata(lsb->m_lua, (void*)lsb);
    lua_pushcclosure(lsb->m_lua, &output, 1);
    lua_setglobal(lsb->m_lua, "output");

    lua_pushlightuserdata(lsb->m_lua, (void*)lsb);
    lua_pushcclosure(lsb->m_lua, &inject_message, 1);
    lua_setglobal(lsb->m_lua, "inject_message");
    lua_sethook(lsb->m_lua, instruction_manager, LUA_MASKCOUNT,
                lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_LIMIT]);

    if (luaL_dofile(lsb->m_lua, lsb->m_lua_file) != 0) {
        snprintf(lsb->m_error_message, ERROR_SIZE, "%s",
                 lua_tostring(lsb->m_lua, -1));
        sandbox_terminate(lsb);
        return 3;
    } else {
        lua_gc(lsb->m_lua, LUA_GCCOLLECT, 0);
        lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_CURRENT] =
          instruction_usage(lsb);
        if (lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_CURRENT]
            > lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_MAXIMUM]) {
            lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_MAXIMUM] =
              lsb->m_usage[USAGE_TYPE_INSTRUCTION][USAGE_STAT_CURRENT];
        }
        lsb->m_status = STATUS_RUNNING;
        if (data_file != NULL && strlen(data_file) > 0) {
            return restore_global_data(lsb, data_file);
        }
    }
    return 0;
}
Ejemplo n.º 10
0
lua_State *lua_loadscript(char *file) {
  char fullpath[LUA_PATHLEN];
  int top;
  lua_State *l;
  lua_list *n;
  char buf[1024];
  void *args[2];

  if(!cpath || !suffix)
    return NULL;

  strlcpy(buf, file, sizeof(buf));

  delchars(buf, "./\\;");

  if(lua_scriptloaded(buf))
    return NULL;

  l = lua_newstate(lua_nsmalloc, NULL);
  if(!l)
    return NULL;

  n = (lua_list *)luamalloc(sizeof(lua_list));;
  if(!n) {
    Error("lua", ERR_ERROR, "Error allocing list for %s.", buf);
    return NULL;
  }

  n->name = getsstring(buf, LUA_PATHLEN);
  if(!n->name) {
    Error("lua", ERR_ERROR, "Error allocing name item for %s.", buf);
    luafree(n);
    return NULL;
  }
  n->calls = 0;

  timerclear(&n->ru_utime);
  timerclear(&n->ru_stime);

  lua_loadlibs(l);
  lua_registerdebug(l);
  lua_registercommands(l);
  lua_registerlocalcommands(l);
  lua_registerdbcommands(l);
  lua_registersocketcommands(l);
  lua_registercryptocommands(l);
  lua_registerschedulercommands(l);

  args[0] = file;
  args[1] = l;
  triggerhook(HOOK_LUA_LOADSCRIPT, args);

#ifdef LUA_USEJIT
  lua_require(l, "lib/jit");
#endif

  lua_require(l, "lib/bootstrap");

  snprintf(fullpath, sizeof(fullpath), "%s/%s%s", cpath->content, file, suffix->content);
  if(luaL_loadfile(l, fullpath)) {
    Error("lua", ERR_ERROR, "Error loading %s.", file);
    lua_close(l);
    freesstring(n->name);
    luafree(n);
    return NULL;
  }

  n->l = l;

  n->next = NULL;
  n->prev = lua_tail;
  n->nicks = NULL;
  n->sockets = NULL;
  n->schedulers = NULL;

  if(!lua_head) { 
    lua_head = n;
  } else {
    lua_tail->next = n;
  }

  lua_tail = n;

  top = lua_gettop(l);

  if(lua_pcall(l, 0, 0, 0)) {
    Error("lua", ERR_ERROR, "Error pcalling: %s.", file);
    lua_close(l);
    freesstring(n->name);

    if(lua_head == n)
      lua_head = NULL;

    lua_tail = n->prev;
    if(lua_tail)
      lua_tail->next = NULL;

    luafree(n);
    return NULL;
  }

  lua_settop(l, top);

  Error("lua", ERR_INFO, "Loaded %s.", file);
  lua_onload(l);

  return l;
}
Ejemplo n.º 11
0
int main(int argc, char** argv)
{
	td_bin_allocator bin_alloc;
	const char *homedir;
	char exepath[512];
	int res, rc, i;
	lua_State* L;

	td_init_portable();

	if (0 != td_get_executable_path(exepath, sizeof exepath)) {
		fprintf(stderr, "couldn't find path to tundra executable");
		return 1;
	}

	if (NULL == (homedir = td_init_homedir())) {
		fprintf(stderr, "couldn't find tundra home dir");
		return 1;
	}

	td_bin_allocator_init(&bin_alloc);

	L = lua_newstate(td_lua_alloc, &bin_alloc);
	if (!L)
		exit(1);

	lua_atpanic(L, on_lua_panic);

	luaL_openlibs(L);

	tundra_open(L);

#if defined(TD_STANDALONE)
	/* this is equivalent to table.insert(package.loaders, 1, td_load_embedded_file) */

	/* get the function */
	lua_getglobal(L, "table");
	lua_getfield(L, -1, "insert");
	lua_remove(L, -2);
	assert(!lua_isnil(L, -1));

	/* arg1: the package.loaders table */
	lua_getglobal(L, "package");
	lua_getfield(L, -1, "loaders");
	lua_remove(L, -2);
	assert(!lua_isnil(L, -1));

	lua_pushinteger(L, 1); /* arg 2 */
	lua_pushcfunction(L, td_load_embedded_file); /* arg 3 */

	lua_call(L, 3, 0);
#endif

	/* setup package.path */
	{
		char ppath[1024];
		snprintf(ppath, sizeof(ppath),
			"%s" TD_PATHSEP_STR "scripts" TD_PATHSEP_STR "?.lua;"
			"%s" TD_PATHSEP_STR "lua" TD_PATHSEP_STR "etc" TD_PATHSEP_STR "?.lua", homedir, homedir);
		lua_getglobal(L, "package");
		assert(LUA_TTABLE == lua_type(L, -1));
		lua_pushstring(L, ppath);
		lua_setfield(L, -2, "path");
	}

	/* push our error handler on the stack now (before the chunk to run) */
	lua_pushcclosure(L, get_traceback, 0);

	switch (luaL_loadbuffer(L, boot_snippet, sizeof(boot_snippet)-1, "boot_snippet"))
	{
	case LUA_ERRMEM:
		td_croak("out of memory");
		return 1;
	case LUA_ERRSYNTAX:
		td_croak("syntax error\n%s\n", lua_tostring(L, -1));
		return 1;
	}

	lua_pushstring(L, homedir);

	lua_newtable(L);
	lua_pushstring(L, exepath);
	lua_rawseti(L, -2, 1);
	for (i=1; i<argc; ++i)
	{
		lua_pushstring(L, argv[i]);
		lua_rawseti(L, -2, i + 1);
	}

	{
		double t2;
		script_call_t1 = td_timestamp();
		res = lua_pcall(L, /*narg:*/2, /*nres:*/0, /*errorfunc:*/ -4);
		t2 = td_timestamp();
		if (global_tundra_stats)
			printf("total time spent in tundra: %.4fs\n", t2 - script_call_t1);
	}

	if (res == 0)
	{
		rc = global_tundra_exit_code;
	}
	else
	{
		fprintf(stderr, "%s\n", lua_tostring(L, -1));
		rc = 1;
	}

	lua_close(L);

	td_bin_allocator_cleanup(&bin_alloc);

	return rc;
}