/* * Compile the source files supplied on the command line. This will compile in-memory and optionally also save to * module files. */ static int interpretFiles(EcCompiler *cp, MprList *files, int argc, char **argv, cchar *className, cchar *methodName) { Ejs *ejs; mprAssert(files); ejs = cp->ejs; ejs->argc = argc; ejs->argv = argv; if (ecCompile(cp, files->length, (char**) files->items, 0) < 0) { return EJS_ERR; } if (cp->run && cp->errorCount == 0) { ejs->flags &= ~EJS_FLAG_COMPILER; ejsEnableGC(ejs, 1); ejsCollectGarbage(ejs, EJS_GC_ALL); if (ejsRunProgram(ejs, className, methodName) < 0) { ejsReportError(ejs, "Error in program"); return EJS_ERR; } } return 0; }
/* Load the standard ejs modules with an optional override search path and list of required modules. If the require list is empty, then ejs->empty will be true. This routine should only be called once for an interpreter. */ int ejsLoadModules(Ejs *ejs, cchar *search, MprList *require) { EjsService *sp; sp = ejs->service; assure(mprGetListLength(ejs->modules) == 0); ejs->empty = !(require == 0 || mprGetListLength(require)); if (search) { initSearchPath(ejs, search); } lock(sp); if (loadRequiredModules(ejs, require) < 0) { if (ejs->exception) { ejsReportError(ejs, "Cannot initialize interpreter"); } ejsDestroyVM(ejs); unlock(sp); return MPR_ERR_CANT_READ; } unlock(sp); if (mprHasMemError(ejs)) { mprError("Memory allocation error during initialization"); ejsDestroyVM(ejs); return MPR_ERR_MEMORY; } assure(!ejs->exception); return 0; }
/* * Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; if (ecOpenConsoleStream(cp->lexer, (cmd) ? commandGets: consoleGets) < 0) { mprError(cp, "Can't open input"); return EJS_ERR; } if (cmd) { cp->lexer->input->stream->buf = mprStrdup(cp, cmd); } else { cp->interactive = 1; } cp->input = cp->lexer->input; cp->token = cp->lexer->input->token; ecResetInput(cp); tmpArgv[0] = EC_INPUT_STREAM; while (!cp->lexer->input->stream->eof) { err = 0; cp->uid = 0; if (ecCompile(cp, 1, tmpArgv, 0) < 0) { ejs->result = ejs->undefinedValue; err++; } if (cp->run && !err && cp->errorCount == 0) { if (ejsRun(ejs) < 0) { ejsReportError(ejs, "Error in script"); } } if (!ejs->exception && ejs->result != ejs->undefinedValue) { if (ejs->result->primitive || ejsIsDate(ejs->result)) { if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) { mprPrintf(cp, "%s\n", result->value); } } else { if ((result = (EjsString*) ejsToJson(ejs, ejs->result)) != 0) { mprPrintf(cp, "%s\n", result->value); } } } ecResetInput(cp); cp->errorCount = 0; err = 0; } ecCloseStream(cp->lexer); return 0; }
/* Interpret from the console or from a literal command */ static int interpretCommands(EcCompiler *cp, cchar *cmd) { Ejs *ejs; EjsString *result; char *tmpArgv[1]; int err; ejs = cp->ejs; cp->interactive = 1; if (ecOpenConsoleStream(cp, (cmd) ? commandGets: consoleGets, cmd) < 0) { mprError("Cannot open input"); return EJS_ERR; } tmpArgv[0] = EC_INPUT_STREAM; while (!cp->stream->eof && !mprIsStopping()) { err = 0; cp->uid = 0; ejs->result = ESV(undefined); if (ecCompile(cp, 1, tmpArgv) < 0) { mprRawLog(0, "%s", cp->errorMsg); ejs->result = ESV(undefined); err++; } if (!err && cp->errorCount == 0) { if (ejsRunProgram(ejs, NULL, NULL) < 0) { ejsReportError(ejs, "Error in script"); } } if (!ejs->exception && ejs->result != ESV(undefined)) { if (ejsIs(ejs, ejs->result, Date) /* MOB || ejsIsType(ejs, ejs->result) */) { if ((result = (EjsString*) ejsToString(ejs, ejs->result)) != 0) { mprPrintf("%@\n", result); } } else if (ejs->result != ESV(null)) { if ((result = (EjsString*) ejsSerialize(ejs, ejs->result, EJS_JSON_SHOW_PRETTY)) != 0) { mprPrintf("%@\n", result); } } } ecResetInput(cp); cp->errorCount = 0; cp->fatalError = 0; } ecCloseStream(cp); return 0; }
/* Compile the source files supplied on the command line. This will compile in-memory and optionally also save to module files. */ static int interpretFiles(EcCompiler *cp, MprList *files, int argc, char **argv, cchar *className, cchar *method) { Ejs *ejs; assert(files); ejs = cp->ejs; if (ecCompile(cp, files->length, (char**) files->items) < 0) { mprLog("ejs", 0, "%s\n", cp->errorMsg); return EJS_ERR; } if (cp->errorCount == 0) { if (ejsRunProgram(ejs, className, method) < 0) { ejsReportError(ejs, "Error in program"); return EJS_ERR; } } return 0; }
/* Compile the source files supplied on the command line. This will compile in-memory and optionally also save to module files. */ static int interpretFiles(EcCompiler *cp, MprList *files, int argc, char **argv, cchar *className, cchar *method) { Ejs *ejs; assure(files); MPR_VERIFY_MEM(); ejs = cp->ejs; if (ecCompile(cp, files->length, (char**) files->items) < 0) { mprRawLog(0, "%s\n", cp->errorMsg); return EJS_ERR; } assure(ejs->result == 0 || (MPR_GET_GEN(MPR_GET_MEM(ejs->result)) != MPR->heap->dead)); if (cp->errorCount == 0) { if (ejsRunProgram(ejs, className, method) < 0) { ejsReportError(ejs, "Error in program"); return EJS_ERR; } } return 0; }
/* * Compile the source files supplied on the command line. This will compile in-memory and optionally also save to * module files. */ static int interpretFiles(EcCompiler *cp, MprList *files, int argc, char **argv, cchar *className, cchar *methodName, int lang) { Ejs *ejs; mprAssert(files); ejs = cp->ejs; ejs->argc = argc; ejs->argv = argv; if (ecCompile(cp, files->length, (char**) files->items, 0) < 0) { return EJS_ERR; } if (cp->run && cp->errorCount == 0) { if (ejsRunProgram(ejs, className, methodName) < 0) { ejsReportError(ejs, "Error in program"); return EJS_ERR; } } return 0; }
/* One-line embedding. Evaluate a script. This will compile and interpret the given script. */ int ejsEvalScript(cchar *script) { Ejs *ejs; mprCreate(0, 0, 0); if ((ejs = ejsCreateVM(0, 0, 0)) == 0) { mprDestroy(0); return MPR_ERR_MEMORY; } mprAddRoot(ejs); if (ejsLoadModules(ejs, 0, 0) < 0) { mprDestroy(0); return MPR_ERR_CANT_READ; } if (ejsLoadScriptLiteral(ejs, ejsCreateStringFromAsc(ejs, script), NULL, EC_FLAGS_NO_OUT | EC_FLAGS_DEBUG) < 0) { ejsReportError(ejs, "Error in program"); mprDestroy(0); return MPR_ERR; } mprDestroy(MPR_EXIT_DEFAULT); return 0; }
/* One-line embedding. Evaluate a file. This will compile and interpret the given Ejscript source file. */ int ejsEvalFile(cchar *path) { Ejs *ejs; mprCreate(0, 0, 0); if ((ejs = ejsCreateVM(0, 0, 0)) == 0) { mprDestroy(0); return MPR_ERR_MEMORY; } mprAddRoot(ejs); if (ejsLoadModules(ejs, 0, 0) < 0) { mprDestroy(0); return MPR_ERR_CANT_READ; } if (ejsLoadScriptFile(ejs, path, NULL, EC_FLAGS_NO_OUT | EC_FLAGS_DEBUG) < 0) { ejsReportError(ejs, "Error in program"); mprDestroy(0); return MPR_ERR; } mprDestroy(MPR_EXIT_DEFAULT); return 0; }
MAIN(ejsMain, int argc, char **argv, char **envp) { Mpr *mpr; Ejs *ejs; EcCompiler *ec; char *argp, *searchPath, *path, *homeDir; int nextArg, err, flags; /* Initialize Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddStandardSignals(); if (mprStart(mpr) < 0) { mprError("Cannot start mpr services"); return EJS_ERR; } err = 0; searchPath = 0; argc = mpr->argc; argv = (char**) mpr->argv; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--chdir") || smatch(argp, "--home") || smatch(argp, "-C")) { if (nextArg >= argc) { err++; } else { homeDir = argv[++nextArg]; if (chdir((char*) homeDir) < 0) { mprError("Cannot change directory to %s", homeDir); } } } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--log")) { if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (smatch(argp, "--name")) { /* Just ignore. Used to tag commands with a unique command line */ nextArg++; } else if (smatch(argp, "--search") || smatch(argp, "--searchpath")) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { mprStartLogging("stderr:1", 0); mprSetCmdlineLogging(1); } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); return 0; } else { /* Ignore */ } } path = mprJoinPath(mprGetAppDir(), mprGetPathBase(argv[0])); path = mprReplacePathExt(path, ".es"); mprAddRoot(path); argv[0] = path; if ((ejs = ejsCreateVM(argc, (cchar **) &argv[0], 0)) == 0) { return MPR_ERR_MEMORY; } app->ejs = ejs; if (ejsLoadModules(ejs, searchPath, NULL) < 0) { return MPR_ERR_CANT_READ; } mprLog(2, "Load script \"%s\"", path); flags = EC_FLAGS_BIND | EC_FLAGS_DEBUG | EC_FLAGS_NO_OUT | EC_FLAGS_THROW; if ((ec = ecCreateCompiler(ejs, flags)) == 0) { return MPR_ERR_MEMORY; } mprAddRoot(ec); ecSetOptimizeLevel(ec, 9); ecSetWarnLevel(ec, 1); if (ecCompile(ec, 1, (char**) &path) < 0) { if (flags & EC_FLAGS_THROW) { ejsThrowSyntaxError(ejs, "%s", ec->errorMsg ? ec->errorMsg : "Cannot parse script"); ejsReportError(ejs, "Error in script"); } err = MPR_ERR; } else { mprRemoveRoot(ec); if (ejsRunProgram(ejs, NULL, NULL) < 0) { ejsReportError(ejs, "Error in script"); err = MPR_ERR; } } if (!err) { err = mpr->exitStatus; } app->ejs = 0; mprTerminate(MPR_EXIT_DEFAULT, err); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }