예제 #1
0
int sqlang_sr_init_child(void)
{
	memset(&_sr_J_env, 0, sizeof(sr_sqlang_env_t));
	_sr_J_env.J = sq_open(1024);
	if(_sr_J_env.J==NULL) {
		LM_ERR("cannot create SQlang context (exec)\n");
		return -1;
	}
    sq_pushroottable(_sr_J_env.J);
	/*sets the print functions*/
	sq_setprintfunc(_sr_J_env.J, sqlang_printfunc, sqlang_errorfunc);
	//sq_setnativedebughook(_sr_J_env.J, sqlang_debughook);
	sq_enabledebuginfo(_sr_J_env.J, 1);

    sqstd_register_bloblib(_sr_J_env.J);
    sqstd_register_iolib(_sr_J_env.J);
    sqstd_register_systemlib(_sr_J_env.J);
    sqstd_register_mathlib(_sr_J_env.J);
    sqstd_register_stringlib(_sr_J_env.J);
	sqstd_seterrorhandlers(_sr_J_env.J);

	sqlang_sr_kemi_register_libs(_sr_J_env.J);
	if(_sr_sqlang_load_file.s != NULL && _sr_sqlang_load_file.len>0) {
		_sr_J_env.JJ = sq_open(1024);
		if(_sr_J_env.JJ==NULL) {
			LM_ERR("cannot create load SQLang context (load)\n");
			return -1;
		}
		sq_pushroottable(_sr_J_env.JJ);
		LM_DBG("*** sqlang top index now is: %d\n", (int)sqlang_gettop(_sr_J_env.JJ));
		/*sets the print functions*/
		sq_setprintfunc(_sr_J_env.JJ, sqlang_printfunc, sqlang_errorfunc);
		//sq_setnativedebughook(_sr_J_env.JJ, sqlang_debughook);
		sq_enabledebuginfo(_sr_J_env.JJ, 1);

		sqstd_register_bloblib(_sr_J_env.JJ);
		sqstd_register_iolib(_sr_J_env.JJ);
		sqstd_register_systemlib(_sr_J_env.JJ);
		sqstd_register_mathlib(_sr_J_env.JJ);
		sqstd_register_stringlib(_sr_J_env.JJ);
		sqstd_seterrorhandlers(_sr_J_env.JJ);

		sqlang_sr_kemi_register_libs(_sr_J_env.JJ);
		LM_DBG("loading sqlang script file: %.*s\n",
				_sr_sqlang_load_file.len, _sr_sqlang_load_file.s);
		if(sqlang_kemi_load_script()<0) {
			return -1;
		}
	}
	LM_DBG("JS initialized!\n");
	return 0;
}
예제 #2
0
파일: sqwrap.cpp 프로젝트: comarius/pizu
void SqEnv::_init(size_t sz)
{

   // AutoLock a(&_m);
    __vm = sq_open(sz);
    _vm= &__vm;

    assert( *_vm );

    Sqrat::DefaultVM::Set(*_vm);
    sq_setprintfunc(*_vm, SqEnv::print_func, SqEnv::print_func);
    sq_newclosure(*_vm, SqEnv::error_handler,0);
    sq_seterrorhandler(*_vm);
    //sq
    sq_pushroottable(*_vm);
    sqstd_register_iolib(*_vm);
    sqstd_register_bloblib(*_vm);
    sqstd_register_mathlib(*_vm);
    sqstd_register_stringlib(*_vm);
    sqstd_register_systemlib(*_vm);

    sqstd_seterrorhandlers(*_vm);
    sqstd_printcallstack(*_vm);

//    setnativedebughook(_vmsys,debug_hook);
    sq_notifyallexceptions(*_vm, true);
}
예제 #3
0
int main(int argc, char* argv[]) 
{ 
	HSQUIRRELVM v; 
	v = sq_open(1024); // creates a VM with initial stack size 1024 

	//REGISTRATION OF STDLIB
	//sq_pushroottable(v); //push the root table where the std function will be registered
	//sqstd_register_iolib(v);  //registers a library
	// ... call here other stdlibs string,math etc...
	//sq_pop(v,1); //pops the root table
	//END REGISTRATION OF STDLIB
	
	sqstd_seterrorhandlers(v); //registers the default error handlers

	sq_setprintfunc(v, printfunc,errorfunc); //sets the print function

	sq_pushroottable(v); //push the root table(were the globals of the script will be stored)
	if(SQ_SUCCEEDED(sqstd_dofile(v, _SC("test.nut"), SQFalse, SQTrue))) // also prints syntax errors if any 
	{
		call_foo(v,1,2.5,_SC("teststring"));
	}

	sq_pop(v,1); //pops the root table
	sq_close(v); 

	return 0; 
} 
예제 #4
0
파일: sqfunc.cpp 프로젝트: Ruw-Van/krkrz
/// vm 初期化
HSQUIRRELVM init() {
    vm = sq_open(1024);
    sq_pushroottable(vm);
    sqstd_register_mathlib(vm);
    sqstd_register_stringlib(vm);
    sqstd_seterrorhandlers(vm);
    sq_pop(vm,1);
    return vm;
}
예제 #5
0
int main(int argc, char *argv[])
{
    setbuf(stdout, NULL);

    if (argc < 2) {
        scprintf(_SC("SQDBG error : no file specified"));
        return -1;
    }

    HSQUIRRELVM v = sq_open(1024);
    sqstd_seterrorhandlers(v);

    //!! INITIALIZES THE DEBUGGER ON THE TCP PORT 1234
    //!! ENABLES AUTOUPDATE
    HSQREMOTEDBG rdbg = sq_rdbg_init(v, 1234, SQTrue);
    if (rdbg) {

        //!! ENABLES DEBUG INFO GENERATION(for the compiler)
        sq_enabledebuginfo(v, SQTrue);

        sq_setprintfunc(v, printfunc, errorfunc);

        //!! SUSPENDS THE APP UNTIL THE DEBUGGER CLIENT CONNECTS
        if (SQ_SUCCEEDED(sq_rdbg_waitforconnections(rdbg))) {
            scprintf(_SC("connected\n"));

            const SQChar *fname = NULL;
#ifdef _UNICODE
            SQChar sTemp[256];
            mbstowcs(sTemp,argv[1],(int)strlen(argv[1])+1);
            fname=sTemp;
#else
            fname = argv[1];
#endif
            //!!REGISTERS STANDARDS LIBS
            sq_pushroottable(v);
            sqstd_register_bloblib(v);
            sqstd_register_iolib(v);
            //!!EXECUTE A SCTIPT
            if (SQ_FAILED(sqstd_dofile(v, fname, SQFalse, SQTrue))) {
                PrintError(v);
#if defined(_WIN32) || defined(_WIN64)
                _getch();
#else
                getch();
#endif
            }
        }
        //!! CLEANUP
        sq_rdbg_shutdown(rdbg);
    }
    else {
        PrintError(v);
    }
    sq_close(v);
    return 0;
}
예제 #6
0
/*
 * Initialize Squirrel VM
 */
void initSQVM(HSQUIRRELVM v) {
	sqstd_seterrorhandlers(v);
	sq_setprintfunc(v, sq_printfunc, sq_errorfunc);
	sq_pushroottable(v);
	sqstd_register_systemlib(v);
#ifndef EMO_WITH_SANDBOX
	sqstd_register_iolib(v);
#endif
	sqstd_register_bloblib(v);
	sqstd_register_mathlib(v);
	sqstd_register_stringlib(v);
}
예제 #7
0
파일: sq.cpp 프로젝트: apaikan/myCub
int sqrl_main(int argc, char* argv[])
{
    
	HSQUIRRELVM v;
	SQInteger retval = 0;
	const SQChar *filename=NULL;
#if defined(_MSC_VER) && defined(_DEBUG)
	_CrtSetAllocHook(MemAllocHook);
#endif
	
	v=sq_open(1024);
	sq_setprintfunc(v,printfunc,errorfunc);

	sq_pushroottable(v);
    
	sqstd_register_bloblib(v);    
	sqstd_register_iolib(v);
	sqstd_register_mathlib(v);
 	   
    /*
     * TODO: system and string library should be implemented for nuttx 
     *       they need 'longjmp' and 'setjmp'. 
     */   
    //sqstd_register_stringlib(v);    
    //sqstd_register_systemlib(v);

	//aux library
	//sets error handlers
	sqstd_seterrorhandlers(v);

	//gets arguments
	switch(getargs(v,argc,argv,&retval))
	{
	case _INTERACTIVE:
		Interactive(v);
		break;
	case _DONE:
	case _ERROR:
	default: 
		break;
	}

	sq_close(v);
	
#if defined(_MSC_VER) && defined(_DEBUG)
	_getch();
	_CrtMemDumpAllObjectsSince( NULL );
#endif
	return retval;
    
    return 0;
}
예제 #8
0
CSquirrel::CSquirrel(CResource* pResource)
{
	m_pResource = pResource;

	// Create the VM
	m_pVM = sq_open(DEFAULT_STACK_SIZE);

	// Was the VM created successfully?
	if( m_pVM )
	{
		// Push the root table onto the stack
		sq_pushroottable(m_pVM);

		// Register the default error handles
		sqstd_seterrorhandlers(m_pVM);

		// Set the print and error functions
		sq_setprintfunc(m_pVM, printfunc, errorfunc);
		
		// Register the blob library
		sqstd_register_bloblib(m_pVM);

		// Register the input/out library
		sqstd_register_iolib(m_pVM);

		// Register the math library
		sqstd_register_mathlib(m_pVM);

		// Register the string library
		sqstd_register_stringlib(m_pVM);

		// Register the system library
		sqstd_register_systemlib(m_pVM);

		// Register our shared functions

		// Register our own functions
#ifdef _SERVER
		CPlayerNatives::Register(this);
		CVehicleNatives::Register(this);
		C3DLabelNatives::Register(this);
		CActorNatives::Register(this);
		CCheckpointNatives::Register(this);
		CObjectNatives::Register(this);
		CServerNatives::Register(this);
		CBlipNatives::Register(this);
#endif
	}
}
예제 #9
0
void SquirrelVM::Init()
{
    _CallState = -1;
	_VM = sq_open(1024);
	sq_setprintfunc(_VM,SquirrelVM::PrintFunc,SquirrelVM::ErrorFunc);
	sq_pushroottable(_VM);
	//sqstd_register_iolib(_VM);
	//sqstd_register_bloblib(_VM);
	sqstd_register_mathlib(_VM);
	sqstd_register_stringlib(_VM);
	sqstd_seterrorhandlers(_VM);
    _root = new SquirrelObject(_VM);
	_root->AttachToStackObject(-1);
	sq_pop(_VM,1);
	//TODO error handler compier error handler
}
예제 #10
0
bool CSquirrel::Load(String strName, String strPath)
{
	// Check if the script exists
	if(!SharedUtility::Exists(strPath.Get()))
		return false;

	// Set the script name
	m_strName = strName;

	// Set the script path
	m_strPath = strPath;

	// Create a squirrel VM with an initial stack size of 1024 bytes (stack will resize as needed)
	m_pVM = sq_open(1024);

	// Register the default error handlers
	sqstd_seterrorhandlers(m_pVM);

	// Set the print function and error function
	sq_setprintfunc(m_pVM, PrintFunction, PrintFunction);

	// Set the compiler error function
	sq_setcompilererrorhandler(m_pVM, CompilerErrorFunction);

	// Set our error handler
	sq_newclosure(m_pVM, PrintErrorFunction, 0);
	sq_seterrorhandler(m_pVM);

	// Push the root table onto the stack
	sq_pushroottable(m_pVM);

#ifdef _SERVER
	// Register the input/output library
	sqstd_register_iolib(m_pVM);
#endif

	// Register the blob library
	sqstd_register_bloblib(m_pVM);

	// Register the math library
	sqstd_register_mathlib(m_pVM);

	// Register the string library
	sqstd_register_stringlib(m_pVM);
	return true;
}
예제 #11
0
HSQUIRRELVM Sq_Open(const char *File) {
  HSQUIRRELVM v; 
  v = sq_open(1024);
  sqstd_seterrorhandlers(v);
  sq_setprintfunc(v, Sq_PrintFunc,Sq_ErrorFunc);

  sq_pushroottable(v);
  sqstd_register_mathlib(v);
  sqstd_register_stringlib(v);
  sq_pop(v,1);

  sq_pushroottable(v);
  sq_pushstring(v, "api", -1);
  sq_newtable(v);
  sq_newslot(v,-3,SQFalse);
  sq_pushstring(v, "api", -1);
  sq_get(v, -2);

  Sq_RegisterFunc(v,   Sq_StrToL,         "Num",            2, "si");
  Sq_RegisterFunc(v,   Sq_MessageBox,     "MessageBox",     1, "s");
  Sq_RegisterFunc(v,   Sq_AskForInput,    "AskForInput",    1, "s");
  Sq_RegisterFunc(v,   Sq_GetLevelJSON,   "GetLevelJSON",   1, "si.");
  Sq_RegisterFunc(v,   Sq_SetLevelJSON,   "SetLevelJSON",   2, "s.");
  Sq_RegisterFunc(v,   Sq_GetLayerRects,  "GetLayerRects",  2, "s");
  Sq_RegisterFunc(v,   Sq_SetLayerRects,  "SetLayerRects",  2, "sa");
  Sq_RegisterFunc(v,   Sq_GetLayerMap,    "GetLayerMap",   -1, "siiii");
  Sq_RegisterFunc(v,   Sq_SetLayerMap,    "SetLayerMap",   -2, "saiiii");
  Sq_RegisterFunc(v,   Sq_RegisterExport, "RegisterExport", 2, "cs");
  Sq_RegisterFunc(v,   Sq_RegisterMenu,   "RegisterMenu",   2, "cs");
  Sq_RegisterFunc(v,   Sq_ExportOpen,     "ExportOpen",     1, "s");
  Sq_RegisterFunc(v,   Sq_ExportWrite,    "ExportWrite",    2, "is");
  Sq_RegisterFunc(v,   Sq_ExportClose,    "ExportClose",    1, "i");
  Sq_RegisterFunc(v,   Sq_GetInfo,        "GetInfo",        1, "s");
  Sq_RegisterFunc(v,   Sq_GetLevelInt,    "GetLevelInt",    2, "is");
  Sq_RegisterFunc(v,   Sq_GetLevelStr,    "GetLevelStr",    2, "ss");
  Sq_RegisterFunc(v,   Sq_GetLevelTbl,    "GetLevelTbl",    1, "s");
  Sq_RegisterFunc(v,   Sq_DecodeJSON,     "DecodeJSON",     1, "s");
  Sq_RegisterFunc(v,   Sq_Interpolate,    "Interpolate",    3, "ssa");
  sq_pop(v,1); // pop api table

  Sq_DoFile(v, "data/common.nut");

  if(File) Sq_DoFile(v, File);
  return v;
}
예제 #12
0
int main(int argc, char* argv[])
{
	HSQUIRRELVM v;
	
	const SQChar *filename=NULL;
#if defined(_MSC_VER) && defined(_DEBUG)
	_CrtSetAllocHook(MemAllocHook);
#endif
	
	v=sq_open(1024);
	sq_setprintfunc(v,printfunc);

	sq_pushroottable(v);

	sqstd_register_bloblib(v);
	sqstd_register_iolib(v);
	sqstd_register_systemlib(v);
	sqstd_register_mathlib(v);
	sqstd_register_stringlib(v);

	//aux library
	//sets error handlers
	sqstd_seterrorhandlers(v);

	//gets arguments
	switch(getargs(v,argc,argv))
	{
	case _INTERACTIVE:
		Interactive(v);
		break;
	case _DONE:
	default: 
		break;
	}

	sq_close(v);
	
#if defined(_MSC_VER) && defined(_DEBUG)
	_getch();
	_CrtMemDumpAllObjectsSince( NULL );
#endif
	return 0;
}
예제 #13
0
CSquirrelVM::CSquirrelVM(CResource * pResource)
	: IScriptVM(pResource),
	m_iStackIndex(2)
{
	m_pVM = sq_open(1024);


	// Register the default error handlers
	sqstd_seterrorhandlers(m_pVM);

	// Push the root table onto the stack
	sq_pushroottable(m_pVM);

	sqstd_register_stringlib(m_pVM);

	sqstd_register_mathlib(m_pVM);

	sqstd_register_systemlib(m_pVM);

	sq_setprintfunc(m_pVM, PrintFunction, PrintFunction);
}
SquirrelScriptContext* SquirrelScriptContext::CreateContext(int stackSize)
{
	SquirrelScriptContext* context = new SquirrelScriptContext();

	HSQUIRRELVM vm = sq_open(stackSize);
	vm->user_data = context;

	sq_setprintfunc(vm, SquirrelPrintCallback);
	sqstd_seterrorhandlers(vm);

	// Push root table so stdlibs can register themselves to it
	sq_pushroottable(vm);

	// Register stdlibs
	sqstd_register_iolib(vm);
	sqstd_register_mathlib(vm);
	sqstd_register_stringlib(vm);
	sqstd_register_systemlib(vm);

	context->m_vm = vm;

	return context;
}
예제 #15
0
파일: SqWrap.cpp 프로젝트: exhu/sqwrap
SqWrap::SqWrap() {
    vm = sq_open(1024);
    sqstd_seterrorhandlers(vm);
    sq_setprintfunc(vm, printfunc, errorfunc);
}
예제 #16
0
파일: main.cpp 프로젝트: ruifig/nutcracker
int main(int argc, char *argv[])
{
	if(argc < 2){
		scprintf(_SC("SQDBG error : no file specified"));
		return -1;
	}

	// RVF +
	int debuggerPort = 0;
	for (int i = 1; i < argc; i++) {
		const char* arg = argv[i];
		if (!strncmp(arg, "-d", 2))
			debuggerPort = std::atoi(arg + 2);
	}

	if (!debuggerPort) {
		scprintf(_SC("SQDBG error : Debugger port not specified. Use the -d<PORT> parameter"));
		return EXIT_FAILURE;
	}
	// RVF -

	HSQUIRRELVM v = sq_open(1024);
	sqstd_seterrorhandlers(v);

	//!! INITIALIZES THE DEBUGGER ON THE TCP PORT 1234
	//!! ENABLES AUTOUPDATE
	HSQREMOTEDBG rdbg = sq_rdbg_init(v,debuggerPort,SQTrue);
	if(rdbg) {

		//!! ENABLES DEBUG INFO GENERATION(for the compiler)
		sq_enabledebuginfo(v,SQTrue);

		sq_setprintfunc(v,printfunc,errorfunc);

		//!! SUSPENDS THE APP UNTIL THE DEBUGGER CLIENT CONNECTS
		if(SQ_SUCCEEDED(sq_rdbg_waitforconnections(rdbg))) {
			scprintf(_SC("connected\n"));

			const SQChar *fname=NULL;
#ifdef _UNICODE
			SQChar sTemp[256];
			mbstowcs(sTemp,argv[argc-1],(int)strlen(argv[1])+1);
			fname=sTemp;
#else
			fname=argv[argc-1];
#endif 
			//!!REGISTERS STANDARDS LIBS
			sq_pushroottable(v);
			sqstd_register_bloblib(v);
			sqstd_register_iolib(v);
			sqstd_register_systemlib(v);
			sqstd_register_mathlib(v);
			sqstd_register_stringlib(v);
			//!!EXECUTE A SCTIPT
			if(SQ_FAILED(sqstd_dofile(v,fname,SQFalse,SQTrue))) {
				PrintError(v);
				_getch();
			}
		}
		//!! CLEANUP
		sq_rdbg_shutdown(rdbg);
	}
	else {
		PrintError(v);
	}
	sq_close(v);
	return 0;
}
예제 #17
0
파일: main.cpp 프로젝트: ruifig/nutcracker
int main (int argc, char** argv)
{
	ENABLE_LEAK_CHECK();

#if defined(SHELL_PLATFORM_WINDOWS)
	SetConsoleTitle("Squirrel Shell " SHELL_VERSION_STR " (" SHELL_CPUARCH ")");
#else
	stderrIsRedirected = !isatty(2);
#endif

	// Parse command line arguments.
	const char* fileName	= NULL;
	bool		interactive = argc == 1;
	int			firstArg	= 0,
				i;
	bool isDebug = false;
	int debuggerPort = 0;
	for (i = 1; argv[i]; ++i)
	{
		char* arg = argv[i];

		if (!strcmp(arg, "-h") || !strcmp(arg, "--help"))
		{
			printf("Squirrel Shell %s for %s on %s (" __DATE__ ")\n"
				   SHELL_VERSION_COPYRIGHT "\n"
				   "\n"
				   "This is free software, and comes WITHOUT ANY WARRANTY; without even the implied\n"
				   "warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU\n"
				   "General Public License for more details.\n"
				   "\n"
				   "MD5 hash calculation code (c) Colin Plumb\n"
				   "PCRE (c) University of Cambridge\n"
				   "Squirrel (c) Alberto Demichelis\n"
				   "zlib (c) Jean-loup Gailly and Mark Adler\n"
				   "\n"
				   "Usage:\n"
				   "   squirrelsh [options] [script_file] [script_arguments]\n"
				   "\n"
				   "Options:\n"
				   "   -h, --help          Display this text\n"
				   "   -d<PORT>            Enable the debugger, in the specified port\n"
				   "   -i, --interactive   Run shell in interactive mode\n"
				   "                       If script file is specified, it will be executed before\n"
				   "                       entering this mode\n"
				   "   -s, --silent        Do not display error and warning messages\n"
				   "   -v, --version       Display shell version number\n"
				   "\n"
				   "Examples:\n"
				   "   squirrelsh                    Run shell in interactive mode\n"
				   "   squirrelsh foo.nut            Run foo.nut script without arguments\n"
				   "   squirrelsh -i foo.nut 1 2 3   Run foo.nut script with arguments \"1\", \"2\"\n"
				   "                                 and \"3\", and switch into interactive mode\n",
				   SHELL_VERSION_STR,
				   SHELL_PLATFORM,
				   SHELL_CPUARCH);

			return EXIT_SUCCESS;
		}
		else if (!strncmp(arg, "-d", 2))
		{
			// RVF +
			isDebug = true;
			debuggerPort = std::atoi(arg + 2);
			if (debuggerPort == 0)
			{
				printf("No debugger port specified\n");
				return EXIT_FAILURE;
			}
			// RVF -
		}
		else if (!strcmp(arg, "-i") || !strcmp(arg, "--interactive"))
			interactive = true;
		else if (!strcmp(arg, "-v") || !strcmp(arg, "--version"))
		{
			printf("%s\n", SHELL_VERSION_STR);
			return EXIT_SUCCESS;
		}
		else if (!strcmp(arg, "-s") || !strcmp(arg, "--silent"))
			silent = true;
		else
		{
			// First unreserved argument will be treated as script file name.
			fileName = arg;
			firstArg = i;
			break;
		}
	}

	if (!fileName && !interactive)
	{
		PrintError("ERROR: Script file not specified.\n");
		return EXIT_FAILURE;
	}

	// Initialize Squirrel.
	sqvm = sq_open(1024);
	if (!sqvm)
	{
		PrintError("ERROR: Failed to create Squirrel VM.\n");
		return EXIT_FAILURE;
	}

	sqstd_seterrorhandlers(sqvm);

	HSQREMOTEDBG rdbg = nullptr;
	if (isDebug)
	{
		rdbg = sq_rdbg_init(sqvm, debuggerPort, SQTrue);
		sq_enabledebuginfo(sqvm, SQTrue);

		//!! SUSPENDS THE APP UNTIL THE DEBUGGER CLIENT CONNECTS
		scprintf("Waiting for the debugger to connect...\n");
		if (!SQ_SUCCEEDED(sq_rdbg_waitforconnections(rdbg)))
		{
			PrintError("ERROR: Failed to connect to the debugger.\n");
			return EXIT_FAILURE;
		}
		scprintf(_SC("Connected to the debugger\n"));
	}


	//sq_setcompilererrorhandler(sqvm, SquirrelCompileError);

	_RPT0(_CRT_WARN, "--- Squirrel initialized\n");

	// Register some globals.
	SetSqString("SHELL_VERSION", SHELL_VERSION_STR, SQTrue);
	SetSqString("SQUIRREL_VERSION", SQUIRREL_VERSION_SHORT, SQTrue);
	SetSqString("PLATFORM", SHELL_PLATFORM, SQTrue);
	SetSqString("CPU_ARCH", SHELL_CPUARCH, SQTrue);

	// Initialize libraries.
	Init_Base();
	Init_IO();
	Init_File();
	Init_Math();
	Init_Util();
	Init_Hash();
	Init_RegExp();

	_RPT0(_CRT_WARN, "--- Libraries initialized\n");

	// Set up global variables...
	sq_pushroottable(sqvm);

	// RVF +
	// Initialize squirrel std libraries
	//sqstd_register_bloblib(sqvm);
	sqstd_register_iolib(sqvm); // We need this one because of the handy "dofile" function
	sqstd_register_stringfunctions(sqvm); // This registers only some string functions that are useful and don't clash with Squirrel Shell
	// NOTE: Not registering the other libraries, because there are name clashing between Squirrel Shell and SqStdLib
	//sqstd_register_systemlib(sqvm);
	//sqstd_register_mathlib(sqvm);
	//sqstd_register_stringlib(sqvm);
	// RVF -


	// ... number of command line arguments...
	sq_pushstring(sqvm, "__argc", -1);
	sq_pushinteger(sqvm, SQInteger(argc - firstArg));
	if (SQ_FAILED(sq_newslot(sqvm, -3, SQFalse)))
	{
		PrintError("ERROR: Failed to create \"__argc\" integer value.\n");
		Shutdown();
		return EXIT_FAILURE;
	}

	// ... and arguments themselves.
	sq_pushstring(sqvm, "__argv", -1);
	sq_newarray(sqvm, 0);
	for (i = firstArg; argv[i]; ++i)
	{
		sq_pushstring(sqvm, argv[i], -1);
		sq_arrayappend(sqvm, -2);
	}
	if (SQ_FAILED(sq_newslot(sqvm, -3, SQFalse)))
	{
		PrintError("ERROR: Failed to create \"__argv\" array.\n");
		Shutdown();
		return EXIT_FAILURE;
	}
	sq_pop(sqvm, 1);

	// Load and run script.
	SQInteger result = EXIT_SUCCESS;
	if (fileName && LoadScript(fileName))
	{
		sq_pushroottable(sqvm);
		if (SQ_FAILED(sq_call(sqvm, 1, SQTrue, isDebug ? SQTrue : SQFalse)))
		{
			if (!silent)
			{
				const SQChar* errMsg = "Unknown error.";
				sq_getlasterror(sqvm);
				if (sq_gettype(sqvm, -1) == OT_STRING)
					sq_getstring(sqvm, -1, &errMsg);

				PrintError("ERROR: %s\n", errMsg);
			}
			Shutdown();
			return EXIT_FAILURE;
		}

		// Get script execution result.
		if (sq_getvmstate(sqvm) == SQ_VMSTATE_SUSPENDED)
			result = retCode;
		else
		{
			if (sq_gettype(sqvm, -1) == OT_INTEGER)
				sq_getinteger(sqvm, -1, &result);
		}

		// Pop everything except root table.
		sq_settop(sqvm, 1);
	}

	// Enter interactive mode (if necessary).
	if (interactive)
	{
		SQChar cmd[MAX_CMD_LENGTH + 1];
		do
		{
#if defined(SHELL_PLATFORM_WINDOWS)
			GetCurrentDirectory(sizeof(cmd), cmd);
#else
			getcwd(cmd, sizeof(cmd));
#endif
			cmd[sizeof(cmd) - 1] = 0;

			printf("%s> ", ConvPath(cmd, SQFalse));
			fgets(cmd, MAX_CMD_LENGTH, stdin);
			if (SQ_FAILED(sq_compilebuffer(sqvm, cmd, SQInteger(strlen(cmd)), "", SQTrue)))
				continue;

			sq_pushroottable(sqvm);
			if (SQ_FAILED(sq_call(sqvm, 1, SQFalse, SQFalse)) && !silent)
			{
				const SQChar* errMsg = "Unknown error.";
				sq_getlasterror(sqvm);
				if (sq_gettype(sqvm, -1) == OT_STRING)
					sq_getstring(sqvm, -1, &errMsg);

				PrintError("ERROR: %s\n", errMsg);
			}
		} while(sq_getvmstate(sqvm) != SQ_VMSTATE_SUSPENDED);
		result = retCode;
	}

	if (isDebug)
	{
		sq_rdbg_shutdown(rdbg);
	}

	Shutdown();
	return int(result);
}
예제 #18
0
파일: script.c 프로젝트: edomin/Marathoner
void MTR_ScriptsInit(void)
{
    #ifdef MTR_MOD_PLUGIN
    int i;
    int j;
    #endif

    MTR_LogWrite("Initializing script subsystem", 0, MTR_LMT_INFO);

    MTR_LogWrite_s("Reporting Squirrel compile-time version:", 1, MTR_LMT_INFO,
     SQUIRREL_VERSION);
    MTR_LogWrite_i("Reporting Squirrel linked version:", 1, MTR_LMT_INFO,
     sq_getversion());

    mtrVm = sq_open(1024);
    sq_setprintfunc(mtrVm, MTR_ScriptPrintFunc, MTR_ScriptErrorFunc); //sets the print function
    sq_pushroottable(mtrVm);
    sqstd_register_bloblib(mtrVm);
    sqstd_register_iolib(mtrVm);
    sqstd_register_systemlib(mtrVm);
    sqstd_register_mathlib(mtrVm);
    sqstd_register_stringlib(mtrVm);
    sqstd_seterrorhandlers(mtrVm); //registers the default error handlers
    MTR_LogWrite("Squirrel VM created", 1, MTR_LMT_INFO);
    /* Registering functions and constants from all binding plugins */
    MTR_LogWrite("Registering functions and constants of engine", 1,
     MTR_LMT_INFO);

    MTR_ScriptsRegisterAll();

    MTR_LogWrite("Script functions and constants of engine registered",
     1, MTR_LMT_INFO);
    /* Registering functions and constants from all binding plugins */
    MTR_LogWrite("Registering functions and constants of bindings", 1,
     MTR_LMT_INFO);
    #ifdef MTR_MOD_PLUGIN
    for (i = 0; i < mtrPluginsCount; i++)
    {
        for (j = 0; j < mtrPluginData[i].report->prereqsCount; j++)
        {
            if (strcmp(mtrPluginData[i].report->prereqs[j], "Script_Squirrel") == 0)
            {
                MTR_LogWrite_s("Binding found:", 2, MTR_LMT_INFO,
                 mtrPluginData[i].report->moduleID);
                MTR_PluginInit = (mtrPluginInitFunc)MTR_FindFunction(
                 mtrPluginData[i].report->moduleID, "MTR_PluginInit");
                if (MTR_PluginInit != NULL)
                    MTR_PluginInit();
                else
                    MTR_Notify("Library not contain MTR_PluginInit function", 3,
                     MTR_LMT_ERROR);
            }
        }
    }
    #else
        MTR_BifInitAll();
    #endif

    MTR_LogWrite("Script functions and constants of bindings registered",
     1, MTR_LMT_INFO);
    MTR_LogWrite("Script subsystem initialized", 0, MTR_LMT_INFO);
}
예제 #19
0
CResourceScript::CResourceScript ( CString strFile, int ScriptType, CResource *pResource )
{
	// Warp some varibles.
	m_pResource = pResource;
	m_iScriptType = ScriptType;

	// Format script localization.
	m_strFile.Format("%s\\%s\\%s", m_pResource->GetDirectory().Get(), m_pResource->GetName().Get(), strFile.Get());

	// Check if file exists.
	if(!Exists(m_strFile.Get()))
	{
		CLog::Printf( "[Resources] Script %s not found!", m_strFile.Get() );
		return;
	}

	// Check script type - default server.
	if ( ScriptType == SCRIPT_CLIENT )
	{
		/*
			TODO:
				- Warp to code WebServ,
				- Create class for client resources,
				- ..
		*/
	} else {
		// Create root stack for script.
		m_pVM = sq_open(1024);

		//Register error and print functions.
		sqstd_seterrorhandlers(m_pVM);
		sq_setprintfunc(m_pVM, PrintFunction, ErrorFunction);
		sq_setcompilererrorhandler(m_pVM, CompilerErrorFunction);

		// Push root vm table.
		sq_pushroottable(m_pVM);

		// Register basic systems.
		sqstd_register_systemlib(m_pVM);
		sqstd_register_iolib(m_pVM);
		sqstd_register_bloblib(m_pVM);
		sqstd_register_mathlib(m_pVM);
		sqstd_register_stringlib(m_pVM);

		// Register Squirrel Classes
		m_pResource->RegisterClass(this);
		
		// Register all functions.

		// Event functions.
		CEventNatives::Register(this);

		// Funkcje gracza.
		CPlayerNatives::Register(this);

		if ( SQ_FAILED ( sqstd_dofile(m_pVM, m_strFile.Get(), SQFalse, SQTrue) ) )
		{
			// cannot compile script file.
			return;
		}

		// Define basic varibles.
		CSquirrelArguments pArgs;
		pArgs.push(MAX_PLAYERS);
		RegisterVarible ( "MAX_PLAYERS", &pArgs );
		pArgs.clear();
	}
}