Exemplo n.º 1
0
int main(int argc, char** argv) {
	signal(SIGSEGV, AnsiSignalHandler);
//    printf("SILK V0.1\n");
    if (argc < 2) {
		printf("usage: %s file.js\n", argv[0]);
		exit(1);
    }
	
	char *startup = readFile(argv[1]);
	if (!startup) {
		printf("%s not found\n", argv[1]);
		exit(1);
	}
	if (startup[0] == '#' && startup[1] == '!') {
		startup[0] = startup[1] = '/';
	}
	init_global_object();
	{
		HandleScope scope;
		context = Persistent<Context>::New(Context::New(NULL, globalObject));
		Context::Scope context_scope(context);

//		Debug::EnableAgent("silkjs", 9222);
//		Debug::SetDebugMessageDispatchHandler(debugger, true);

		Handle<Script>init = Script::New(String::New("global=this;"), String::New("builtin"));
		init->Run();
		
		mainScript = Persistent<Script>::New(Script::Compile(String::New(startup), String::New(argv[1])));
		mainScript->Run();
		Handle<String> process_name = String::New("main");
		Handle<Value> process_val = context->Global()->Get(process_name);
		Handle<Function> process_fun = Handle<Function>::Cast(process_val);
		mainFunc = Persistent<Function>::New(process_fun);
		const int ac = argc-2;
		Handle<Value>av[ac];
		for (int i=2; i<argc; i++) {
			av[i-2] = String::New(argv[i]);
		}
//		printf("SILKJS running %s\n", argv[1]);
		mainFunc->Call(context->Global(), ac, av);
	}
}
Exemplo n.º 2
0
int main (int argc, char** argv) {
    signal(SIGSEGV, AnsiSignalHandler);
    //    printf("SILK V0.1\n");
    char *startup;
    const char *progName;
    signal(SIGSEGV, AnsiSignalHandler);
    signal(SIGINT, AnsiSignalHandler);

    if (argc < 2) {
        startup = readFile("/usr/local/silkjs/builtin/interpreter.js");
        if (!startup) {
            startup = readFile("/usr/share/silkjs/builtin/interpreter.js");
        }
        progName = "interpreter";
    }
    else {
        startup = readFile(argv[1]);
        progName = argv[1];
    }
    if (!startup) {
        printf("%s not found\n", argv[1]);
        exit(1);
    }
    if (startup[0] == '#' && startup[1] == '!') {
        startup[0] = startup[1] = '/';
    }
    
    // v8 command line switches
    const char *switches = "--harmony";
    V8::SetFlagsFromString(switches, strlen(switches));
    
    {
        //		Isolate *isolate = Isolate::New();
        //		isolate->Enter();
        //		Locker lock(isolate);
        Locker locker;
        HandleScope scope;

        init_global_object();
        V8::SetCaptureStackTraceForUncaughtExceptions(true, 50); // , StackTrace::kDetailed);
        context = Context::New(NULL, globalObject);
        Context::Scope context_scope(context);
        {
            Locker locker;
            //			Debug::SetDebugMessageDispatchHandler(debugger, true);
            //			Debug::EnableAgent("silkjs", 5858);

            Handle<Script>init = Script::New(String::New("global=this; module = {}; include('builtin/all.js');"), String::New("builtin"));
            init->Run();
            V8::SetCaptureStackTraceForUncaughtExceptions(true, 50, StackTrace::kDetailed);
            TryCatch tryCatch;
            mainScript = Persistent<Script>::New(Script::Compile(String::New(startup), String::New(progName)));
            if (mainScript.IsEmpty()) {
                ReportException(&tryCatch);
                exit(1);
            }
            Handle<Value>v = mainScript->Run();
            if (v.IsEmpty()) {
                ReportException(&tryCatch);
                exit(1);
            }
            Handle<String> process_name = String::New("main");
            Handle<Value> process_val = context->Global()->Get(process_name);
            if (!process_val.IsEmpty() && process_val->IsFunction()) {
                Handle<Function> process_fun = Handle<Function>::Cast(process_val);
                mainFunc = Persistent<Function>::New(process_fun);
                int ac = argc - 2;
                if (ac < 0) {
                    ac = 0;
                }
                Handle<Value>av[ac];
                for (int i = 2; i < argc; i++) {
                    av[i - 2] = String::New(argv[i]);
                }
                v = mainFunc->Call(context->Global(), ac, av);
                if (v.IsEmpty()) {
                    ReportException(&tryCatch);
                    exit(1);
                }
            }
        }
        context.Dispose();
    }
}