📄 ejsglobal.c
字号:
/* * @file ejsGlobal.c * @brief EJS support methods *//********************************* Copyright **********************************//* * @copy default * * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. * Copyright (c) Michael O'Brien, 1994-1995. All Rights Reserved. * * This software is distributed under commercial and open source licenses. * You may use the GPL open source license described below or you may acquire * a commercial license from Mbedthis Software. You agree to be fully bound * by the terms of either license. Consult the LICENSE.TXT distributed with * this software for full details. * * This software is open source; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2 of the License, or (at your * option) any later version. See the GNU General Public License for more * details at: http://www.mbedthis.com/downloads/gplLicense.html * * This program is distributed WITHOUT ANY WARRANTY; without even the * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * * This GPL license does NOT permit incorporating this software into * proprietary programs. If you are unable to comply with the GPL, you must * acquire a commercial license to use this software. Commercial licenses * for this software and support services are available from Mbedthis * Software at http://www.mbedthis.com * * @end *//********************************** Includes **********************************/#include "ejs.h"#if BLD_FEATURE_EJS/******************************************************************************//************************************* Code ***********************************//******************************************************************************//* * assert(condition) */static int assertProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ int b; if (argc < 1) { ejsError(ep, EJS_ARG_ERROR, "usage: assert(condition)"); return -1; } b = ejsVarToBoolean(argv[0]); if (b == 0) { ejsError(ep, EJS_ASSERT_ERROR, "Assertion failure at line %d", ejsGetLineNumber(ep)); return -1; } ejsWriteVarAsBoolean(ep, ep->result, b); return 0;}/******************************************************************************//* * breakpoint(msg) */static int breakpointProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *buf; if (argc < 1) { return 0; } buf = ejsVarToString(ep, argv[0]); if (buf) { mprBreakpoint(0, buf); } return 0;}/******************************************************************************//* * basename(path) */static int basenameProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *path; if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "usage: basename(path)"); return -1; } path = ejsVarToString(ep, argv[0]); if (path == 0) { return MPR_ERR_MEMORY; } ejsSetReturnValueToString(ep, mprGetBaseName(path)); return 0;}/******************************************************************************//* * stripext(path) */static int stripextProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *cp, *path, *stripPath; if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "usage: stripext(path)"); return -1; } path = ejsVarToString(ep, argv[0]); if (path == 0) { return MPR_ERR_MEMORY; } stripPath = mprStrdup(ep, path); if ((cp = strrchr(stripPath, '.')) != 0) { *cp = '\0'; } ejsSetReturnValueToString(ep, stripPath); mprFree(stripPath); return 0;}/******************************************************************************//* * dirname(path) */static int dirnameProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *path; char dirname[MPR_MAX_FNAME]; if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "usage: dirname(path)"); return -1; } path = ejsVarToString(ep, argv[0]); if (path == 0) { return MPR_ERR_MEMORY; } ejsSetReturnValueToString(ep, mprGetDirName(dirname, sizeof(dirname), path)); return 0;}/******************************************************************************//* * trim(string) -- trim white space */static int trimProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *str, *buf, *cp; if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "usage: trim(string)"); return -1; } str = ejsVarToString(ep, argv[0]); if (str == 0) { return MPR_ERR_MEMORY; } str = buf = mprStrdup(ep, str); while (isspace(*str)) { str++; } cp = &str[strlen(str) - 1]; while (cp >= str) { if (isspace(*cp)) { *cp = '\0'; } else { break; } cp--; } ejsSetReturnValueToString(ep, str); mprFree(buf); return 0;}/******************************************************************************//* * Terminate the script */static int exitScript(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ int status; if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "usage: exit(status)"); return -1; } status = (int) ejsVarToInteger(argv[0]); ejsExit(ep, status); ejsWriteVarAsString(ep, ep->result, ""); return 0;}/******************************************************************************//* * include javascript libraries. */ static int includeProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv){ int i; mprAssert(argv); for (i = 0; i < argc; i++) { if (ejsEvalFile(ep, argv[i], 0) < 0) { return -1; } } return 0;}/******************************************************************************//* * include javascript libraries at the global level */ static int includeGlobalProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv){ int fid, i; mprAssert(argv); /* * Create a new block and set the context to be the global scope */ fid = ejsSetBlock(ep, ep->global); for (i = 0; i < argc; i++) { if (ejsEvalFile(ep, argv[i], 0) < 0) { ejsCloseBlock(ep, fid); return -1; } } ejsCloseBlock(ep, fid); return 0;}/******************************************************************************/#if BLD_DEBUG/* * Print variables to stdout */static int printvProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ EjsVar *vp; char *buf; int i; for (i = 0; i < argc; ) { vp = argv[i++]; /* mprPrintf(ep, "arg[%d] = ", i); */ buf = ejsVarToString(ep, vp); if (vp->propertyName == 0 || *vp->propertyName == '\0') { mprPrintf(ep, "%s: ", buf); } else if (i < argc) { mprPrintf(ep, "%s = %s, ", vp->propertyName, buf); } else { mprPrintf(ep, "%s = %s\n", vp->propertyName, buf); } } return 0;}#endif/******************************************************************************//* * Print the args to stdout */static int printProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ char *buf; int i; for (i = 0; i < argc; i++) { buf = ejsVarToString(ep, argv[i]); mprPrintf(ep, "%s", buf); } return 0;}/******************************************************************************//* * println */static int printlnProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ printProc(ep, thisObj, argc, argv); mprPrintf(ep, "\n"); return 0;}/******************************************************************************/#if FUTURE/* * sprintf */static int sprintfProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ va_list ap; char *buf; void **args; int result; if (argc <= 1) { ejsError(ep, EJS_ARG_ERROR, "Usage: sprintf(fmt, [args ...])"); return -1; } args = mprAlloc(ep, sizeof(void*) * (argc - 1)); if (args == 0) { mprAssert(args); return -1; } for (i = 1; i < argc; i++) { args[i - 1] = argv[i]); } va_start(ap, fmt); *buf = 0; result = inner(0, &buf, MPR_MAX_STRING, fmt, args); va_end(ap); ejsSetReturnValueToString(ep, buf); mprFree(buf); return 0;}/******************************************************************************/inner(const char *fmt, void **args){ va_list ap; va_start(ap, fmt); *buf = 0; mprSprintfCore(ctx, &buf, maxSize, fmt, ap, MPR_PRINTF_ARGV); va_end(ap);}#endif/******************************************************************************//* * sleep */static int sleepProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv){ if (argc != 1) { ejsError(ep, EJS_ARG_ERROR, "Usage: sleep(milliseconds)"); return -1; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -