/* 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; EcCompiler *cp; Ejs *ejs; cchar *cmd, *className, *method, *homeDir; char *argp, *searchPath, *modules, *name, *tok, *extraFiles; int nextArg, err, ecFlags, stats, merge, bind, noout, debug, optimizeLevel, warnLevel, strict, i, next; /* 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; className = 0; cmd = 0; method = 0; searchPath = 0; stats = 0; merge = 0; bind = 1; noout = 1; debug = 1; warnLevel = 1; optimizeLevel = 9; strict = 0; app->files = mprCreateList(-1, 0); app->iterations = 1; argc = mpr->argc; argv = (char**) mpr->argv; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (smatch(argp, "--bind")) { bind = 1; } else if (smatch(argp, "--class")) { if (nextArg >= argc) { err++; } else { className = argv[++nextArg]; } } else 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); } } #if BIT_UNIX_LIKE } else if (smatch(argp, "--chroot")) { /* Not documented or supported */ if (nextArg >= argc) { err++; } else { homeDir = mprGetAbsPath(argv[++nextArg]); if (chroot(homeDir) < 0) { if (errno == EPERM) { mprPrintfError("%s: Must be super user to use the --chroot option", mprGetAppName(mpr)); } else { mprPrintfError("%s: Cannot change change root directory to %s, errno %d", mprGetAppName(), homeDir, errno); } return 4; } } #endif } else if (smatch(argp, "--cmd") || smatch(argp, "-c")) { if (nextArg >= argc) { err++; } else { cmd = argv[++nextArg]; } #if BIT_WIN_LIKE } else if (smatch(argp, "--cygroot")) { if (nextArg >= argc) { err++; } else { app->cygroot = sclone(argv[++nextArg]); } #endif } else if (smatch(argp, "--debug")) { debug = 1; } else if (smatch(argp, "--debugger") || smatch(argp, "-D")) { mprSetDebugMode(1); } else if (smatch(argp, "--files") || smatch(argp, "-f")) { /* Compatibility with mozilla shell */ if (nextArg >= argc) { err++; } else { extraFiles = sclone(argv[++nextArg]); name = stok(extraFiles, " \t", &tok); while (name != NULL) { mprAddItem(app->files, sclone(name)); name = stok(NULL, " \t", &tok); } } } else if (smatch(argp, "--iterations") || smatch(argp, "-i")) { if (nextArg >= argc) { err++; } else { app->iterations = atoi(argv[++nextArg]); } } else if (smatch(argp, "--log")) { if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (smatch(argp, "--method")) { if (nextArg >= argc) { err++; } else { method = argv[++nextArg]; } } else if (smatch(argp, "--name")) { /* Just ignore. Used to tag commands with a unique command line */ nextArg++; } else if (smatch(argp, "--nobind")) { bind = 0; } else if (smatch(argp, "--nodebug")) { debug = 0; } else if (smatch(argp, "--optimize")) { if (nextArg >= argc) { err++; } else { optimizeLevel = atoi(argv[++nextArg]); } } else if (smatch(argp, "-s")) { /* Compatibility with mozilla shell. Just ignore */ } else if (smatch(argp, "--search") || smatch(argp, "--searchpath")) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (smatch(argp, "--standard")) { strict = 0; } else if (smatch(argp, "--stats")) { stats = 1; } else if (smatch(argp, "--strict")) { strict = 1; } else if (smatch(argp, "--require")) { if (nextArg >= argc) { err++; } else { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); name = stok(modules, " \t", &tok); while (name != NULL) { require(name); name = stok(NULL, " \t", &tok); } } } else if (smatch(argp, "--verbose") || smatch(argp, "-v")) { mprStartLogging("stderr:2", 0); mprSetCmdlineLogging(1); } else if (smatch(argp, "--version") || smatch(argp, "-V")) { mprPrintf("%s-%s\n", BIT_VERSION, BIT_BUILD_NUMBER); return 0; } else if (smatch(argp, "--warn")) { if (nextArg >= argc) { err++; } else { warnLevel = atoi(argv[++nextArg]); } } else { err++; break; } } if (err) { /* If --method or --class is specified, then the named class.method will be run (method defaults to "main", class defaults to first class with a "main"). Examples: ejs ejs script.es arg1 arg2 arg3 ejs --class "Customer" --method "start" --files "script1.es script2.es" main.es */ mprPrintfError("Usage: %s [options] script.es [arguments] ...\n" " Ejscript shell program options:\n" " --class className # Name of class containing method to run\n" " --cmd ejscriptCode # Literal ejscript statements to execute\n" " --cygroot path # Set cygwin root for resolving script paths\n" " --debug # Use symbolic debugging information (default)\n" " --debugger # Disable timeouts to make using a debugger easier\n" " --files \"files..\" # Extra source to compile\n" " --log logSpec # Internal compiler diagnostics logging\n" " --method methodName # Name of method to run. Defaults to main\n" " --nodebug # Omit symbolic debugging information\n" " --optimize level # Set the optimization level (0-9 default is 9)\n" " --require 'module,...' # Required list of modules to pre-load\n" " --search ejsPath # Module search path\n" " --standard # Default compilation mode to standard (default)\n" " --stats # Print memory stats on exit\n" " --strict # Default compilation mode to strict\n" " --verbose | -v # Same as --log stderr:2 \n" " --version # Emit the compiler version information\n" " --warn level # Set the warning message level (0-9 default is 0)\n\n", mpr->name); return -1; } if ((ejs = ejsCreateVM(argc - nextArg, (cchar **) &argv[nextArg], 0)) == 0) { return MPR_ERR_MEMORY; } app->ejs = ejs; if (ejsLoadModules(ejs, searchPath, app->modules) < 0) { return MPR_ERR_CANT_READ; } ecFlags = 0; ecFlags |= (merge) ? EC_FLAGS_MERGE: 0; ecFlags |= (bind) ? EC_FLAGS_BIND: 0; ecFlags |= (noout) ? EC_FLAGS_NO_OUT: 0; ecFlags |= (debug) ? EC_FLAGS_DEBUG: 0; cp = app->compiler = ecCreateCompiler(ejs, ecFlags); if (cp == 0) { return MPR_ERR_MEMORY; } ecSetRequire(cp, app->modules); ecSetOptimizeLevel(cp, optimizeLevel); ecSetWarnLevel(cp, warnLevel); ecSetStrictMode(cp, strict); if (nextArg < argc) { mprAddItem(app->files, sclone(argv[nextArg])); } if (app->cygroot) { /* When cygwin invokes a script with shebang, it passes a cygwin path to the script The ejs --cygroot option permits ejscript to conver this to a native path */ for (next = 0; (name = mprGetNextItem(app->files, &next)) != 0; ) { if (*name == '/' || *name == '\\') { mprSetItem(app->files, next - 1, sjoin(app->cygroot, name, NULL)); } } } for (i = 0; !err && i < app->iterations; i++) { if (cmd) { if (interpretCommands(cp, cmd) < 0) { err++; } } else if (mprGetListLength(app->files) > 0) { if (interpretFiles(cp, app->files, argc - nextArg, &argv[nextArg], className, method) < 0) { err++; } } else { /* No args - run as an interactive shell */ if (interpretCommands(cp, NULL) < 0) { err++; } } } if (stats) { #if BIT_DEBUG mprSetLogLevel(1); mprPrintMem("Memory Usage", 1); #endif } if (!err) { err = mpr->exitStatus; } app->ejs = 0; mprTerminate(MPR_EXIT_DEFAULT, err); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }
static EjsWorker *initWorker(Ejs *ejs, EjsWorker *worker, Ejs *baseVM, cchar *name, EjsArray *search, cchar *scriptFile) { Ejs *wejs; EjsWorker *self; EjsName sname; static int workerSeqno = 0; ejsBlockGC(ejs); if (worker == 0) { worker = ejsCreateWorker(ejs); } worker->ejs = ejs; worker->state = EJS_WORKER_BEGIN; if (name) { worker->name = sclone(name); } else { lock(ejs); worker->name = sfmt("worker-%d", workerSeqno++); unlock(ejs); } /* Create a new interpreter and an "inside" worker object and pair it with the current "outside" worker. The worker interpreter gets a new dispatcher */ if (baseVM) { if ((wejs = ejsCloneVM(baseVM)) == 0) { ejsThrowMemoryError(ejs); return 0; } } else { if ((wejs = ejsCreateVM(0, 0, ejs->flags)) == 0) { ejsThrowMemoryError(ejs); return 0; } if (ejsLoadModules(wejs, 0, 0) < 0) { return 0; } } worker->pair = self = ejsCreateWorker(wejs); self->state = EJS_WORKER_BEGIN; self->ejs = wejs; self->inside = 1; self->pair = worker; self->name = sjoin("inside-", worker->name, NULL); #if MOB mprEnableDispatcher(wejs->dispatcher); #endif if (search) { ejsSetSearchPath(ejs, (EjsArray*) search); } // TODO - these should be don't delete ejsSetProperty(ejs, worker, ES_Worker_name, ejsCreateStringFromAsc(ejs, self->name)); ejsSetProperty(wejs, self, ES_Worker_name, ejsCreateStringFromAsc(wejs, self->name)); sname = ejsName(wejs, EJS_WORKER_NAMESPACE, "self"); ejsSetPropertyByName(wejs, wejs->global, sname, self); /* Workers have a dedicated namespace to enable viewing of the worker globals (self, onmessage, postMessage...) */ ejsDefineReservedNamespace(wejs, wejs->global, NULL, EJS_WORKER_NAMESPACE); addWorker(ejs, worker); if (scriptFile) { worker->scriptFile = sclone(scriptFile); worker->state = EJS_WORKER_STARTED; if (mprCreateEvent(wejs->dispatcher, "workerMain", 0, (MprEventProc) workerMain, self, 0) < 0) { mprRemoveItem(ejs->workers, worker); ejsThrowStateError(ejs, "Cannot create worker event"); return 0; } } return worker; }
MAIN(ejscMain, int argc, char **argv, char **envp) { Mpr *mpr; MprList *args; Ejs *ejs; EcCompiler *cp; char *argp, *searchPath, *outputFile, *outputDir, *certFile, *name, *tok, *modules; int nextArg, err, ejsFlags, ecFlags, bind, debug, doc, merge, modver; int warnLevel, noout, parseOnly, tabWidth, optimizeLevel, strict; /* Initialize the Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); app = mprAllocObj(App, manageApp); mprAddRoot(app); mprAddStandardSignals(); if (mprStart() < 0) { mprLog("ejsc", 0, "Cannot start mpr services"); return EJS_ERR; } err = 0; searchPath = 0; strict = 0; certFile = 0; ecFlags = 0; bind = 0; debug = 0; doc = 0; merge = 0; modver = 0; noout = 0; parseOnly = 0; tabWidth = 4; warnLevel = 1; outputFile = 0; outputDir = 0; optimizeLevel = 9; for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--bind") == 0) { bind = 1; } else if (strcmp(argp, "--debug") == 0) { debug = 1; } else if (strcmp(argp, "--debugger") == 0 || strcmp(argp, "-D") == 0) { mprSetDebugMode(1); } else if (strcmp(argp, "--dir") == 0) { /* Set the output directory for modules */ if (nextArg >= argc) { err++; } else { outputDir = argv[++nextArg]; } } else if (strcmp(argp, "--doc") == 0) { doc = 1; } else if (strcmp(argp, "--log") == 0) { /* Undocumented logging switch */ if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--merge") == 0) { merge = 1; } else if (strcmp(argp, "--modver") == 0) { if (nextArg >= argc) { err++; } else { modver = ejsParseModuleVersion(argv[++nextArg]); } } else if (strcmp(argp, "--nobind") == 0) { bind = 0; } else if (strcmp(argp, "--noout") == 0) { noout = 1; } else if (strcmp(argp, "--standard") == 0) { strict = 0; } else if (strcmp(argp, "--strict") == 0) { strict = 1; } else if (strcmp(argp, "--optimize") == 0) { if (nextArg >= argc) { err++; } else { optimizeLevel = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--out") == 0) { /* Create a single output module file containing all modules */ if (nextArg >= argc) { err++; } else { outputFile = argv[++nextArg]; } } else if (strcmp(argp, "--parse") == 0) { parseOnly = 1; } else if (strcmp(argp, "--search") == 0 || strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--require") == 0) { if (nextArg >= argc) { err++; } else { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); /* Fix for Xcode and Visual Studio */ if (modules[0] == ' ' || scmp(modules, "null") == 0) { modules[0] = '\0'; } name = stok(modules, " \t,", &tok); while (name != NULL) { require(name); name = stok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--sign") == 0) { if (nextArg >= argc) { err++; } else { certFile = argv[++nextArg]; } #if FUTURE } else if (strcmp(argp, "--strip") == 0) { strip = 1; #endif } else if (strcmp(argp, "--tabWidth") == 0) { if (nextArg >= argc) { err++; } else { tabWidth = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprPrintf("%s\n", ME_TITLE, EJS_VERSION); return 0; } else if (strcmp(argp, "--warn") == 0) { if (nextArg >= argc) { err++; } else { warnLevel = atoi(argv[++nextArg]); } } else if (strcmp(argp, "--web") == 0) { if (app->modules == 0) { app->modules = mprCreateList(-1, 0); } require("ejs"); require("ejs.unix"); require("ejs.db"); // TODO - decouple and remove this require("ejs.db.mapper"); require("ejs.web"); } else { err++; break; } } if (noout || merge) { bind = 1; } if (outputFile && noout) { mprEprintf("Cannot use --out and --noout\n"); err++; } if (argc == nextArg) { err++; } if (err) { /* Usage Examples: ejsc Person.es User.es Customer.es ejsc --out group.mod Person.es User.es Customer.es ejsc --out group.mod Person.es User.es Customer.es NOTE: bind is deliberately not documented and is for internal use only. */ mprEprintf("Usage: %s [options] files...\n" " Ejscript compiler options:\n" " --debug # Include symbolic debugging information in output\n" " --doc # Include documentation strings in output\n" " --dir directory # Set the output directory for modules (default: \".\")\n" " --merge # Merge dependent input modules into the output\n" " --modver version # Set the default module version\n" " --noout # Do not generate any output\n" " --optimize level # Set optimization level (0-9)\n" " --out filename # Name a single output module (default: \"default.mod\")\n" " --parse # Just parse source. No output\n" " --require 'module ...' # List of required modules to pre-load\n" " --search ejsPath # Module search path\n" " --standard # Default compilation mode to standard (default)\n" " --strict # Default compilation mode to strict\n" #if FUTURE " --sign certFile # Sign the module file (not implemented) \n" " --strip # Strip all symbolic names (Cannot import)\n" " --tabwidth # Tab width for '^' error reporting\n" #endif " --version # Emit the compiler version information\n" " --warn level # Set the warning message level (0-9)\n\n", mpr->name); return -1; } ejsFlags = EJS_FLAG_NO_INIT; if (doc) { ejsFlags |= EJS_FLAG_DOC; } if ((ejs = ejsCreateVM(0, 0, ejsFlags)) == 0) { return MPR_ERR_MEMORY; } #if UNUSED mprRunDispatcher(ejs->dispatcher); #endif if (ejsLoadModules(ejs, searchPath, app->modules) < 0) { return MPR_ERR_CANT_READ; } app->ejs = ejs; ecFlags |= (debug) ? EC_FLAGS_DEBUG: 0; ecFlags |= (merge) ? EC_FLAGS_MERGE: 0; ecFlags |= (bind) ? EC_FLAGS_BIND: 0; ecFlags |= (noout) ? EC_FLAGS_NO_OUT: 0; ecFlags |= (parseOnly) ? EC_FLAGS_PARSE_ONLY: 0; ecFlags |= (doc) ? EC_FLAGS_DOC: 0; cp = app->compiler = ecCreateCompiler(ejs, ecFlags); if (cp == 0) { return MPR_ERR_MEMORY; } cp->require = app->modules; cp->modver = modver; ecSetOptimizeLevel(cp, optimizeLevel); ecSetWarnLevel(cp, warnLevel); ecSetStrictMode(cp, strict); ecSetTabWidth(cp, tabWidth); ecSetOutputDir(cp, outputDir); ecSetOutputFile(cp, outputFile); ecSetCertFile(cp, certFile); if (nextArg < argc) { /* Compile the source files supplied on the command line. This will compile in-memory and optionally also save to module files. */ if ((args = expandWild(ejs, argc - nextArg, &argv[nextArg])) == 0) { err++; } else if (ecCompile(cp, args->length, (char**) args->items) < 0) { err++; } if (cp->warningCount > 0 || cp->errorCount > 0) { mprLog("ejs", 0, "%s\n", cp->errorMsg); } } if (cp->errorCount > 0) { err++; } app->ejs = 0; app->compiler = 0; ejsDestroy(ejs); mprDestroy(); return err; }
MAIN(ejsmodMain, int argc, char **argv, char **envp) { Mpr *mpr; EjsMod *mp; Ejs *ejs; MprList *requiredModules; char *argp, *searchPath, *output, *modules, *name, *tok; int nextArg, err, flags; err = 0; output = searchPath = 0; requiredModules = 0; /* Initialze the Multithreaded Portable Runtime (MPR) */ mpr = mprCreate(argc, argv, 0); mprSetAppName(argv[0], 0, 0); /* Allocate the primary control structure */ if ((mp = mprAllocObj(EjsMod, manageMod)) == NULL) { return MPR_ERR_MEMORY; } mprAddRoot(mp); mp->lstRecords = mprCreateList(0, 0); mp->blocks = mprCreateList(0, 0); mp->docDir = sclone("."); mp->outputDir = sclone("."); for (nextArg = 1; nextArg < argc; nextArg++) { argp = argv[nextArg]; if (*argp != '-') { break; } if (strcmp(argp, "--cslots") == 0) { mp->cslots = 1; mp->genSlots = 1; } else if (strcmp(argp, "--debugger") == 0 || strcmp(argp, "-D") == 0) { mprSetDebugMode(1); } else if (strcmp(argp, "--depends") == 0) { mp->depends = 1; } else if (strcmp(argp, "--dir") == 0) { if (nextArg >= argc) { err++; } else { mp->outputDir = sclone(argv[++nextArg]); } } else if (strcmp(argp, "--error") == 0) { /* Undocumented switch */ mp->exitOnError++; mp->warnOnError++; } else if (strcmp(argp, "--html") == 0) { if (nextArg >= argc) { err++; } else { mp->docDir = sclone(argv[++nextArg]); mp->html = 1; } } else if (strcmp(argp, "--listing") == 0) { mp->listing = 1; mp->showAsm = 1; } else if (strcmp(argp, "--log") == 0) { /* Undocumented switch */ if (nextArg >= argc) { err++; } else { mprStartLogging(argv[++nextArg], 0); mprSetCmdlineLogging(1); } } else if (strcmp(argp, "--out") == 0) { if (nextArg >= argc) { err++; } else { output = argv[++nextArg]; mp->cslots = 1; mp->genSlots = 1; } } else if (strcmp(argp, "--search") == 0 || strcmp(argp, "--searchpath") == 0) { if (nextArg >= argc) { err++; } else { searchPath = argv[++nextArg]; } } else if (strcmp(argp, "--version") == 0 || strcmp(argp, "-V") == 0) { mprPrintfError("%s %s-%s\n", BIT_NAME, BIT_VERSION, BIT_NUMBER); return 0; } else if (strcmp(argp, "--require") == 0) { if (nextArg >= argc) { err++; } else { if (requiredModules == 0) { requiredModules = mprCreateList(-1, 0); } modules = sclone(argv[++nextArg]); /* Fix for Xcode and Visual Studio */ if (modules[0] == ' ' || scmp(modules, "null") == 0) { modules[0] = '\0'; } name = stok(modules, " \t", &tok); while (name != NULL) { require(requiredModules, name); name = stok(NULL, " \t", &tok); } } } else if (strcmp(argp, "--warn") == 0) { mp->warnOnError++; } else if (strcmp(argp, "--xml") == 0) { mp->xml = 1; } else { err++; break; } } if (argc == nextArg) { err++; } if (mp->genSlots == 0 && mp->listing == 0 && mp->html == 0 && mp->xml == 0 && mp->depends == 0) { mp->listing = 1; } if (mp->depends && requiredModules == 0) { requiredModules = mprCreateList(-1, 0); } if (err) { /* Examples: ejsmod file.mod # Defaults to --listing ejsmod --listing embedthis.mod ejsmod --out slots.h embedthis.mod */ mprPrintfError("Usage: %s [options] modules ...\n" " Ejscript module manager options:\n" " --cslots # Generate a C slot definitions file\n" " --html dir # Generate HTML documentation to the specified directory\n" " --listing # Create assembler listing files (default)\n" " --out file # Output file for all C slots (implies --cslots)\n" " --require \"modules\" # List of modules to preload\n" " --search ejsPath # Module file search path\n" " --version # Emit the program version information\n" " --warn # Warn about undocumented methods or parameters in doc\n\n", mpr->name); return -1; } /* Need an interpreter to load modules */ flags = EJS_FLAG_NO_INIT; if (mp->html || mp->xml) { flags |= EJS_FLAG_DOC; } if ((ejs = ejsCreateVM(0, 0, flags)) == 0) { return MPR_ERR_MEMORY; } if (ejsLoadModules(ejs, searchPath, requiredModules) < 0) { return MPR_ERR_CANT_READ; } mp->ejs = ejs; if (nextArg < argc) { if (process(mp, output, argc - nextArg, &argv[nextArg]) < 0) { err++; } } if (mp->errorCount > 0) { err = -1; } mprRemoveRoot(mp); ejsDestroyVM(ejs); mprDestroy(MPR_EXIT_DEFAULT); return err; }
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; }