diff options
Diffstat (limited to 'source4/lib/appweb/ejs-2.0/ejs/system')
20 files changed, 3886 insertions, 0 deletions
diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/.ignore b/source4/lib/appweb/ejs-2.0/ejs/system/.ignore new file mode 100755 index 0000000000..fb5a29031e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/.ignore @@ -0,0 +1 @@ +.updated diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/Makefile b/source4/lib/appweb/ejs-2.0/ejs/system/Makefile new file mode 100755 index 0000000000..2d83662655 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/Makefile @@ -0,0 +1,27 @@ +# +# Makefile to build the EJS Object Model +# +# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. +# + +COMPILE := *.c +EXPORT_OBJECTS := yes +MAKE_IFLAGS := -I.. -I../../mpr -I../../exml + +include make.dep + +ifeq ($(BLD_HOST_UNIX),1) +PRE_DIRS = UNIX +else +PRE_DIRS = $(BLD_HOST_OS) +endif + +compileExtra: .updated + +.updated: $(FILES) + @touch .updated + +## Local variables: +## tab-width: 4 +## End: +## vim: tw=78 sw=4 ts=4 diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/README.TXT b/source4/lib/appweb/ejs-2.0/ejs/system/README.TXT new file mode 100644 index 0000000000..a24e0e299c --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/README.TXT @@ -0,0 +1,63 @@ +Embedded JavaScript System Model + + +- Need args, arg types and exceptions thrown +- Error classes + +class Global + class System + class environment + var + class GC + void function run() + function tune() + function getUsedMemory() // Should be properties + function getAllocatedMemory() // Should be properties + + var javascript + var null + var undefined + var true + var false + var Nan + var Infinity + + function random // Not implemented + function sleep // Not implemented + function exit + function yield // Not implemented + + Debug + isDebugMode + + Limits + isLimitsMode // Not implemented + stack // Not implemented + heap // Not implemented + flash // Not implemented + + Memory + getUsedMemory() // Should be properties + getAvailableMemory() // Should be properties + used + flash // Not implemented + + assert() + breakpoint() + dirname() + basename() + eval() + exit() + print() + println() + printVars() + sleep() + sort() + time() + typeof() + include() + trace() + printf() // Not implemented + sprintf() + + diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/.ignore b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/.ignore new file mode 100644 index 0000000000..fb5a29031e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/.ignore @@ -0,0 +1 @@ +.updated diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/Makefile b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/Makefile new file mode 100755 index 0000000000..424747052a --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/Makefile @@ -0,0 +1,21 @@ +# +# Makefile to build the EJS Object Model for WIN +# +# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. +# + +COMPILE := *.c +EXPORT_OBJECTS := yes +MAKE_IFLAGS := -I../.. -I../../../mpr + +include make.dep + +compileExtra: .updated + +.updated: $(FILES) + @touch .updated + +## Local variables: +## tab-width: 4 +## End: +## vim: tw=78 sw=4 ts=4 diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFile.c b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFile.c new file mode 100644 index 0000000000..772303152e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFile.c @@ -0,0 +1,98 @@ +/* + * @file ejsFile.c + * @brief File class for the EJ System Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/* + * Default Constructor + */ + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function open(); + */ + +static int openProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.open()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function close(); + */ + +static int closeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.close()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function read(); + */ + +static int readProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.read()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function write(); + */ + +static int writeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.write()\n"); + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineFileClass(Ejs *ep) +{ + EjsVar *fileClass; + + fileClass = ejsDefineClass(ep, "File", "Object", 0); + if (fileClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ep, fileClass, "open", openProc, 0); + ejsDefineCMethod(ep, fileClass, "close", closeProc, 0); + ejsDefineCMethod(ep, fileClass, "read", readProc, 0); + ejsDefineCMethod(ep, fileClass, "write", writeProc, 0); + + return ejsObjHasErrors(fileClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFileSystem.c b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFileSystem.c new file mode 100755 index 0000000000..7b39c16e4d --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsFileSystem.c @@ -0,0 +1,454 @@ +/* + * @file ejsFileSystem.c + * @brief FileSystem class for the EJ System Object Model + * MOB -- this is almost the same as for Windows. Should common up. + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" +#include <dirent.h> + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function void access(string path); + * MOB - API insufficient. Access for read or write? + */ + +static int accessProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: access(path)"); + return -1; + } + + rc = access(argv[0]->string, 04); + + ejsSetReturnValueToBoolean(ejs, (rc == 0) ? 1 : 0); + return 0; +} + +/******************************************************************************/ +/* + * function void mkdir(string path); + */ + +static int mkdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)"); + return -1; + } + + if (mprMakeDirPath(ejs, argv[0]->string) < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create directory"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void rmdir(string path); + */ + +static int rmdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)"); + return -1; + } + + rc = mprDeleteDir(ejs, argv[0]->string); + + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant remove directory"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void dirList(string path, [bool enumDirs]); + * MOB -- need pattern to match (what about "." and ".." and ".*" + */ + +static int dirListProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + DIR *dir; + struct dirent *dirent; + char path[MPR_MAX_FNAME]; + EjsVar *array, *vp; + uchar enumDirs; + + if (argc < 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: dirList(path)"); + return -1; + } + if (argc == 2) { + enumDirs = ejsVarToBoolean(argv[1]); + } else { + enumDirs = 0; + } + array = ejsCreateArray(ejs, 0); + ejsMakeObjPermanent(array, 1); + + /* + * First collect the files + */ + mprSprintf(path, sizeof(path), "%s/*.*", argv[0]->string); + + dir = opendir(path); + if (dir == 0) { + ejsError(ejs, EJS_ARG_ERROR, "Can't enumerate dirList(path)"); + return -1; + } + + while ((dirent = readdir(dir)) != 0) { + if (dirent->d_name[0] == '.') { + continue; + } + if (!enumDirs || (dirent->d_type & DT_DIR)) { + mprSprintf(path, sizeof(path), "%s/%s", argv[0]->string, + dirent->d_name); + vp = ejsCreateStringVar(ejs, path); + ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY); + ejsFreeVar(ejs, vp); + } + } + + closedir(dir); + + ejsSetReturnValue(ejs, array); + ejsMakeObjPermanent(array, 0); + + /* + * Can free now as the return value holds the reference + */ + ejsFreeVar(ejs, array); + + return 0; +} + +/******************************************************************************/ +/* + * function void getFreeSpace(); + */ + +static int getFreeSpaceProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ +#if UNUSED + MprApp *app; + uint space; + + app = mprGetApp(ejs); + space = IFILEMGR_GetFreeSpace(app->fileMgr, 0); + ejsSetReturnValueToInteger(ejs, space); +#endif + + return 0; +} + +/******************************************************************************/ +/* + * function void writeFile(string path, var data); + */ + +static int writeFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFile *file; + char *data, *buf; + int bytes, length, rc; + + if (argc != 2 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: writeFile(path, var)"); + return -1; + } + + if (ejsVarIsString(argv[1])) { + data = argv[1]->string; + length = argv[1]->length; + buf = 0; + } else { + buf = data = ejsVarToString(ejs, argv[1]); + length = strlen(data); + } + + /* + * Create fails if already present + */ + rc = mprDelete(ejs, argv[0]->string); + file = mprOpen(ejs, argv[0]->string, O_CREAT | O_WRONLY | O_BINARY, 0664); + if (file == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[0]->string); + mprFree(buf); + return -1; + } + + rc = 0; + bytes = mprWrite(file, data, length); + if (bytes != length) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + } + + mprClose(file); + + mprFree(buf); + return rc; +} + +/******************************************************************************/ +/* + * function string readFile(string path); + */ + +static int readFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprApp *app; + MprFile *file; + MprBuf *buf; + char *data; + int bytes, rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: readFile(path)"); + return -1; + } + buf = mprCreateBuf(ejs, MPR_BUF_INCR, MPR_MAX_BUF); + if (buf == 0) { + ejsMemoryError(ejs); + return -1; + } + + data = mprAlloc(ejs, MPR_BUFSIZE); + if (buf == 0) { + mprFree(buf); + ejsMemoryError(ejs); + return -1; + } + + app = mprGetApp(ejs); + file = mprOpen(ejs, argv[0]->string, O_RDONLY, 0664); + if (file == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string); + mprFree(buf); + return -1; + } + + rc = 0; + while ((bytes = mprRead(file, data, MPR_BUFSIZE)) > 0) { + if (mprPutBlockToBuf(buf, data, bytes) != bytes) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + break; + } + } + + ejsSetReturnValueToBinaryString(ejs, (uchar*) mprGetBufStart(buf), + mprGetBufLength(buf)); + + mprClose(file); + mprFree(data); + mprFree(buf); + + return rc; +} + +/******************************************************************************/ +/* + * function void remove(string path); + */ + +static int removeProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: remove(path)"); + return -1; + } + + rc = unlink(argv[0]->string); + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant remove file"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void rename(string from, string to); + */ + +static int renameProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: rename(old, new)"); + return -1; + } + + unlink(argv[1]->string); + rc = rename(argv[0]->string, argv[1]->string); + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant rename file"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void copy(string old, string new); + */ + +static int copyProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFile *from, *to; + char *buf; + uint bytes; + int rc; + + if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: copy(old, new)"); + return -1; + } + + buf = mprAlloc(ejs, MPR_BUFSIZE); + if (buf == 0) { + ejsMemoryError(ejs); + return -1; + } + + from = mprOpen(ejs, argv[0]->string, O_RDONLY | O_BINARY, 0664); + if (from == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string); + mprFree(buf); + return -1; + } + + to = mprOpen(ejs, argv[1]->string, O_CREAT | O_BINARY, 0664); + if (to == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[1]->string); + mprClose(from); + mprFree(buf); + return -1; + } + + rc = 0; + while ((bytes = mprRead(from, buf, MPR_BUFSIZE)) > 0) { + if (mprWrite(to, buf, bytes) != bytes) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + break; + } + } + + mprClose(from); + mprClose(to); + mprFree(buf); + + return rc; +} + +/******************************************************************************/ +/* + * function FileInfo getFileInfo(string path); + * + * MOB -- should create a real class FileInfo + */ + +static int getFileInfoProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFileInfo info; + EjsVar *fileInfo; + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: getFileInfo(path)"); + return -1; + } + + fileInfo = ejsCreateObjVar(ejs); + if (fileInfo == 0) { + ejsMemoryError(ejs); + return -1; + } + ejsMakeObjPermanent(fileInfo, 1); + + rc = mprGetFileInfo(ejs, argv[0]->string, &info); + if (rc < 0) { + ejsMakeObjPermanent(fileInfo, 0); + ejsFreeVar(ejs, fileInfo); + ejsError(ejs, EJS_IO_ERROR, "Cant get file info for %s", + argv[0]->string); + return -1; + } + + ejsSetPropertyToInteger(ejs, fileInfo, "created", info.ctime); + ejsSetPropertyToInteger(ejs, fileInfo, "length", info.size); + ejsSetPropertyToBoolean(ejs, fileInfo, "isDir", info.isDir); + + ejsSetReturnValue(ejs, fileInfo); + ejsMakeObjPermanent(fileInfo, 0); + + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineFileSystemClass(Ejs *ejs) +{ + EjsVar *fileSystemClass; + + fileSystemClass = ejsDefineClass(ejs, "FileSystem", "Object", 0); + if (fileSystemClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ejs, fileSystemClass, "access", accessProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "mkdir", mkdirProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "rmdir", rmdirProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "dirList", dirListProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "writeFile", writeFileProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "readFile", readFileProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "remove", removeProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "rename", renameProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "copy", copyProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "getFileInfo", getFileInfoProc, 0); + + // MOB -- should be a property with accessor + ejsDefineCMethod(ejs, fileSystemClass, "getFreeSpace", getFreeSpaceProc, 0); + + return ejsObjHasErrors(fileSystemClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsHTTP.c b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsHTTP.c new file mode 100755 index 0000000000..25821f6960 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/UNIX/ejsHTTP.c @@ -0,0 +1,488 @@ +/* + * @file ejsHTTP.c + * @brief HTTP class for the EJ System Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +#if UNUSED +/*********************************** Defines **********************************/ + +#define EJS_WEB_PROPERTY "-web" +#define EJS_HTTP_PROPERTY "-http" + +#define EJS_HTTP_DISPOSED 550 + +/* + * Control structure for one HTTP request structure + */ +typedef struct HTTPControl { + Ejs *ejs; + IWebResp *webResp; + AEECallback *callback; + MprBuf *buf; + EjsVar *thisObj; + char *url; + MprTime requestStarted; + uint timeout; +} HTTPControl; + +/****************************** Forward Declarations **************************/ + +static void cleanup(HTTPControl *hp); +static int createWeb(Ejs *ejs, EjsVar *thisObj); +static void brewCallback(HTTPControl *hp); +static int httpDestructor(Ejs *ejs, EjsVar *vp); +static void httpCallback(HTTPControl *hp, int responseCode); +static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv); + +/******************************************************************************/ +/* + * Constructor + */ + +int ejsHTTPConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 0 && argc != 2) { + ejsError(ejs, EJS_ARG_ERROR, + "Bad usage: HTTP([obj = this, method = onComplete]);"); + return -1; + } + + if (createWeb(ejs, thisObj) < 0) { + return -1; + } + + setCallback(ejs, thisObj, argc, argv); + return 0; +} + +/******************************************************************************/ + +static int createWeb(Ejs *ejs, EjsVar *thisObj) +{ + MprApp *app; + void *web; + + app = mprGetApp(ejs); + + /* + * Create one instance of IWeb for the entire application. Do it here + * so only widgets that require HTTP incurr the overhead. + */ + web = mprGetKeyValue(ejs, "bpWeb"); + if (web == 0) { + if (ISHELL_CreateInstance(app->shell, AEECLSID_WEB, &web) != SUCCESS) { + ejsError(ejs, EJS_IO_ERROR, "Can't create IWEB"); + return -1; + } + } + mprSetKeyValue(ejs, "bpWeb", web); + return 0; +} + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function setCallback(obj, methodString); + */ + +static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc >= 1) { + ejsSetProperty(ejs, thisObj, "obj", argv[0]); + } else { + ejsSetProperty(ejs, thisObj, "obj", thisObj); + } + + if (argc >= 2) { + ejsSetProperty(ejs, thisObj, "method", argv[1]); + } else { + ejsSetPropertyToString(ejs, thisObj, "method", "onComplete"); + } + + return 0; +} + +/******************************************************************************/ +/* + * function fetch(); + */ + +static int fetchProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + HTTPControl *hp; + EjsProperty *pp; + MprApp *app; + IWeb *web; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: fetch(url)"); + return -1; + } + + app = mprGetApp(ejs); + web = (IWeb*) mprGetKeyValue(ejs, "bpWeb"); + + /* + * Web options + * + * WEBOPT_USERAGENT (char*) sets user agent + * WEBOPT_HANDLERDATA (void*) + * WEBOPT_CONNECTTIMEOUT (uint) msec + * WEBOPT_CONTENTLENGTH (long) + * WEBOPT_IDLECONNTIMEOUT (int) + * WEBOPT_ACTIVEXACTIONST (uint) Number of active requests + * + * WEBREQUEST_REDIRECT redirect transparently + * + */ + + hp = mprAllocType(ejs, HTTPControl); + if (hp == 0) { + ejsMemoryError(ejs); + return -1; + } + + hp->ejs = ejs; + hp->buf = mprCreateBuf(hp, MPR_BUF_INCR, MPR_MAX_BUF); + if (hp->buf == 0) { + mprFree(hp); + ejsMemoryError(ejs); + return -1; + } + + /* + * We copy thisObj because we need to preserve both the var and the object. + * We pass the var to brewCallback and so it must persist. The call to + * ejsMakeObjPermanent will stop the GC from collecting the object. + */ + hp->thisObj = ejsDupVar(ejs, thisObj, EJS_SHALLOW_COPY); + ejsSetVarName(ejs, hp->thisObj, "internalHttp"); + + /* + * Must keep a reference to the http object + */ + ejsMakeObjPermanent(hp->thisObj, 1); + + /* + * Make a property so we can access the HTTPControl structure from other + * methods. + */ + pp = ejsSetPropertyToPtr(ejs, thisObj, EJS_HTTP_PROPERTY, hp, 0); + ejsMakePropertyEnumerable(pp, 0); + ejsSetObjDestructor(ejs, hp->thisObj, httpDestructor); + + hp->url = mprStrdup(hp, argv[0]->string); + + hp->timeout = ejsGetPropertyAsInteger(ejs, thisObj, "timeout"); + mprGetTime(hp, &hp->requestStarted); + + hp->callback = mprAllocTypeZeroed(hp, AEECallback); + CALLBACK_Init(hp->callback, brewCallback, hp); + + hp->webResp = 0; + IWEB_GetResponse(web, + (web, &hp->webResp, hp->callback, hp->url, + WEBOPT_HANDLERDATA, hp, + WEBOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)", + WEBOPT_CONNECTTIMEOUT, hp->timeout, + WEBOPT_COPYOPTS, TRUE, + WEBOPT_CONTENTLENGTH, 0, + WEBOPT_END)); + + ejsSetPropertyToString(ejs, thisObj, "status", "active"); + + return 0; +} + +/******************************************************************************/ +/* + * Called whenver the http object is deleted. + */ + +static int httpDestructor(Ejs *ejs, EjsVar *thisObj) +{ + HTTPControl *hp; + + /* + * If the httpCallback has run, then this property will not exist + */ + hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY); + + if (hp) { + cleanup(hp); + } + + return 0; +} + +/******************************************************************************/ +/* + * Stop the request immediately without calling the callback + */ + +static int stopProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + HTTPControl *hp; + + hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY); + + if (hp) { + cleanup(hp); + } + + return 0; +} + +/******************************************************************************/ +/* + * Brew HTTP callback. Invoked for any return data. + */ + +static void brewCallback(HTTPControl *hp) +{ + Ejs *ejs; + EjsVar *thisObj; + ISource *source; + WebRespInfo *info; + char data[MPR_BUF_INCR]; + int bytes; + + mprAssert(hp); + mprAssert(hp->webResp); + + info = IWEBRESP_GetInfo(hp->webResp); + + if (info == 0) { + mprAssert(info); + /* should not happen */ + return; + } + + ejs = hp->ejs; + thisObj = hp->thisObj; + + if (! WEB_ERROR_SUCCEEDED(info->nCode)) { + ejsSetPropertyToString(ejs, thisObj, "status", "error"); + httpCallback(hp, info->nCode); + return; + } + + if (hp->timeout) { + if (mprGetTimeRemaining(hp, hp->requestStarted, hp->timeout) <= 0) { + ejsSetPropertyToString(ejs, thisObj, "status", "timeout"); + httpCallback(hp, 504); + return; + } + } + + /* + * Normal success + */ + source = info->pisMessage; + mprAssert(source); + + bytes = ISOURCE_Read(source, data, sizeof(data)); + + switch (bytes) { + case ISOURCE_WAIT: // No data yet + ISOURCE_Readable(source, hp->callback); + break; + + case ISOURCE_ERROR: + ejsSetPropertyToString(ejs, thisObj, "status", "error"); + httpCallback(hp, info->nCode); + break; + + case ISOURCE_END: + mprAddNullToBuf(hp->buf); + ejsSetPropertyToString(ejs, thisObj, "status", "complete"); + httpCallback(hp, info->nCode); + break; + + default: + if (bytes > 0) { + if (mprPutBlockToBuf(hp->buf, data, bytes) != bytes) { + ejsSetPropertyToString(ejs, thisObj, "status", "partialData"); + httpCallback(hp, 500); + } + } + ISOURCE_Readable(source, hp->callback); + break; + } +} + +/******************************************************************************/ +/* + * Invoke the HTTP completion method + */ + +static void httpCallback(HTTPControl *hp, int responseCode) +{ + Ejs *ejs; + EjsVar *thisObj, *callbackObj; + MprArray *args; + char *msg; + const char *callbackMethod; + + mprAssert(hp); + mprAssert(hp->webResp); + + thisObj = hp->thisObj; + ejs = hp->ejs; + + ejsSetPropertyToInteger(ejs, thisObj, "responseCode", responseCode); + if (mprGetBufLength(hp->buf) > 0) { + ejsSetPropertyToBinaryString(ejs, thisObj, "responseData", + mprGetBufStart(hp->buf), mprGetBufLength(hp->buf)); + } + + callbackObj = ejsGetPropertyAsVar(ejs, thisObj, "obj"); + callbackMethod = ejsGetPropertyAsString(ejs, thisObj, "method"); + + if (callbackObj != 0 && callbackMethod != 0) { + + args = mprCreateItemArray(ejs, EJS_INC_ARGS, EJS_MAX_ARGS); + mprAddItem(args, ejsDupVar(ejs, hp->thisObj, EJS_SHALLOW_COPY)); + + if (ejsRunMethod(ejs, callbackObj, callbackMethod, args) < 0) { + msg = ejsGetErrorMsg(ejs); + mprError(ejs, MPR_LOC, "HTTP callback failed. Details: %s", msg); + } + ejsFreeMethodArgs(ejs, args); + + } else if (ejsRunMethod(ejs, thisObj, "onComplete", 0) < 0) { + msg = ejsGetErrorMsg(ejs); + mprError(ejs, MPR_LOC, "HTTP onComplete failed. Details: %s", msg); + } + + cleanup(hp); +} + +/******************************************************************************/ +/* + * Cleanup + */ + +static void cleanup(HTTPControl *hp) +{ + Ejs *ejs; + MprApp *app; + int rc; + + mprAssert(hp); + mprAssert(hp->webResp); + + ejs = hp->ejs; + + if (hp->webResp) { + rc = IWEBRESP_Release(hp->webResp); + // mprAssert(rc == 0); + hp->webResp = 0; + } + + if (hp->callback) { + CALLBACK_Cancel(hp->callback); + mprFree(hp->callback); + hp->callback = 0; + } + + /* + * Once the property is deleted, then if the destructor runs, it will + * notice that the EJS_HTTP_PROPERTY is undefined. + */ + ejsDeleteProperty(ejs, hp->thisObj, EJS_HTTP_PROPERTY); + + /* + * Allow garbage collection to work on thisObj + */ + ejsMakeObjPermanent(hp->thisObj, 0); + ejsFreeVar(ejs, hp->thisObj); + + mprFree(hp->buf); + mprFree(hp->url); + + mprFree(hp); + + app = mprGetApp(ejs); + + + ISHELL_SendEvent(app->shell, (AEECLSID) app->classId, EVT_USER, 0, 0); +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineHTTPClass(Ejs *ejs) +{ + EjsVar *httpClass; + + httpClass = + ejsDefineClass(ejs, "HTTP", "Object", ejsHTTPConstructor); + if (httpClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ejs, httpClass, "fetch", fetchProc, 0); + ejsDefineCMethod(ejs, httpClass, "stop", stopProc, 0); + ejsDefineCMethod(ejs, httpClass, "setCallback", setCallback, 0); + +#if FUTURE + ejsDefineCMethod(ejs, httpClass, "put", put, 0); + ejsDefineCMethod(ejs, httpClass, "upload", upload, 0); + ejsDefineCMethod(ejs, httpClass, "addUploadFile", addUploadFile, 0); + ejsDefineCMethod(ejs, httpClass, "addPostData", addPostData, 0); + ejsDefineCMethod(ejs, httpClass, "setUserPassword", setUserPassword, 0); + ejsDefineCMethod(ejs, httpClass, "addCookie", addCookie, 0); +#endif + + /* + * Define properties + */ + ejsSetPropertyToString(ejs, httpClass, "status", "inactive"); + + /* This default should come from player.xml */ + + ejsSetPropertyToInteger(ejs, httpClass, "timeout", 30 * 1000); + ejsSetPropertyToInteger(ejs, httpClass, "responseCode", 0); + + return ejsObjHasErrors(httpClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +void ejsTermHTTPClass(Ejs *ejs) +{ + IWeb *web; + int rc; + + web = (IWeb*) mprGetKeyValue(ejs, "bpWeb"); + if (web) { + rc = IWEB_Release(web); + mprAssert(rc == 0); + } +} + +#endif +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/WIN/.ignore b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/.ignore new file mode 100644 index 0000000000..fb5a29031e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/.ignore @@ -0,0 +1 @@ +.updated diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/WIN/Makefile b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/Makefile new file mode 100755 index 0000000000..424747052a --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/Makefile @@ -0,0 +1,21 @@ +# +# Makefile to build the EJS Object Model for WIN +# +# Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. +# + +COMPILE := *.c +EXPORT_OBJECTS := yes +MAKE_IFLAGS := -I../.. -I../../../mpr + +include make.dep + +compileExtra: .updated + +.updated: $(FILES) + @touch .updated + +## Local variables: +## tab-width: 4 +## End: +## vim: tw=78 sw=4 ts=4 diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFile.c b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFile.c new file mode 100644 index 0000000000..24c521891e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFile.c @@ -0,0 +1,98 @@ +/* + * @file ejsFile.c + * @brief File class for the EJScript System Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/* + * Default Constructor + */ + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function open(); + */ + +static int openProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.open()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function close(); + */ + +static int closeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.close()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function read(); + */ + +static int readProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.read()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function write(); + */ + +static int writeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "File.write()\n"); + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineFileClass(Ejs *ep) +{ + EjsVar *fileClass; + + fileClass = ejsDefineClass(ep, "File", "Object", 0); + if (fileClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ep, fileClass, "open", openProc, 0); + ejsDefineCMethod(ep, fileClass, "close", closeProc, 0); + ejsDefineCMethod(ep, fileClass, "read", readProc, 0); + ejsDefineCMethod(ep, fileClass, "write", writeProc, 0); + + return ejsObjHasErrors(fileClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFileSystem.c b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFileSystem.c new file mode 100755 index 0000000000..66c3b84870 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsFileSystem.c @@ -0,0 +1,456 @@ +/* + * @file ejsFileSystem.c + * @brief FileSystem class for the EJ System Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2003-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/* + * Default Constructor + */ + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function void access(string path); + * MOB - API insufficient. Access for read or write? + */ + +static int accessProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: access(path)"); + return -1; + } + + rc = access(argv[0]->string, 04); + + ejsSetReturnValueToBoolean(ejs, (rc == 0) ? 1 : 0); + return 0; +} + +/******************************************************************************/ +/* + * function void mkdir(string path); + */ + +static int mkdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)"); + return -1; + } + + if (mprMakeDirPath(ejs, argv[0]->string) < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create directory"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void rmdir(string path); + */ + +static int rmdirProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: mkdir(path)"); + return -1; + } + + rc = mprDeleteDir(ejs, argv[0]->string); + + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant remove directory"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void dirList(string path, [bool enumDirs]); + * MOB -- need pattern to match (what about "." and ".." and ".*" + */ + +static int dirListProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + WIN32_FIND_DATA findData; + HANDLE h; + char path[MPR_MAX_FNAME]; + EjsVar *array, *vp; + uchar enumDirs; + + if (argc < 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: dirList(path)"); + return -1; + } + if (argc == 2) { + enumDirs = ejsVarToBoolean(argv[1]); + } else { + enumDirs = 0; + } + array = ejsCreateArray(ejs, 0); + ejsMakeObjPermanent(array, 1); + + /* + * First collect the files + */ + mprSprintf(path, sizeof(path), "%s/*.*", argv[0]->string); + h = FindFirstFile(path, &findData); + if (h == INVALID_HANDLE_VALUE) { + ejsError(ejs, EJS_ARG_ERROR, "Can't enumerate dirList(path)"); + return -1; + } + + do { + if (findData.cFileName[0] == '.') { + continue; + } + if (!enumDirs || + (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) { + mprSprintf(path, sizeof(path), "%s/%s", argv[0]->string, + findData.cFileName); + vp = ejsCreateStringVar(ejs, path); + ejsAddArrayElt(ejs, array, vp, EJS_SHALLOW_COPY); + ejsFreeVar(ejs, vp); + } + } while (FindNextFile(h, &findData) != 0); + + FindClose(h); + + ejsSetReturnValue(ejs, array); + ejsMakeObjPermanent(array, 0); + + /* + * Can free now as the return value holds the reference + */ + ejsFreeVar(ejs, array); + + return 0; +} + +/******************************************************************************/ +/* + * function void getFreeSpace(); + */ + +static int getFreeSpaceProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ +#if UNUSED + MprApp *app; + uint space; + + app = mprGetApp(ejs); + space = IFILEMGR_GetFreeSpace(app->fileMgr, 0); + ejsSetReturnValueToInteger(ejs, space); +#endif + + return 0; +} + +/******************************************************************************/ +/* + * function void writeFile(string path, var data); + */ + +static int writeFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFile *file; + char *data, *buf; + int bytes, length, rc; + + if (argc != 2 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: writeFile(path, var)"); + return -1; + } + + if (ejsVarIsString(argv[1])) { + data = argv[1]->string; + length = argv[1]->length; + buf = 0; + } else { + buf = data = ejsVarToString(ejs, argv[1]); + length = strlen(data); + } + + /* + * Create fails if already present + */ + rc = mprDelete(ejs, argv[0]->string); + file = mprOpen(ejs, argv[0]->string, O_CREAT | O_WRONLY | O_BINARY, 0664); + if (file == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[0]->string); + mprFree(buf); + return -1; + } + + rc = 0; + bytes = mprWrite(file, data, length); + if (bytes != length) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + } + + mprClose(file); + + mprFree(buf); + return rc; +} + +/******************************************************************************/ +/* + * function string readFile(string path); + */ + +static int readFileProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprApp *app; + MprFile *file; + MprBuf *buf; + char *data; + int bytes, rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: readFile(path)"); + return -1; + } + buf = mprCreateBuf(ejs, MPR_BUF_INCR, MPR_MAX_BUF); + if (buf == 0) { + ejsMemoryError(ejs); + return -1; + } + + data = mprAlloc(ejs, MPR_BUFSIZE); + if (buf == 0) { + mprFree(buf); + ejsMemoryError(ejs); + return -1; + } + + app = mprGetApp(ejs); + file = mprOpen(ejs, argv[0]->string, O_RDONLY, 0664); + if (file == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string); + mprFree(buf); + return -1; + } + + rc = 0; + while ((bytes = mprRead(file, data, MPR_BUFSIZE)) > 0) { + if (mprPutBlockToBuf(buf, data, bytes) != bytes) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + break; + } + } + + ejsSetReturnValueToBinaryString(ejs, mprGetBufStart(buf), + mprGetBufLength(buf)); + + mprClose(file); + mprFree(data); + mprFree(buf); + + return rc; +} + +/******************************************************************************/ +/* + * function void remove(string path); + */ + +static int removeProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: remove(path)"); + return -1; + } + + rc = unlink(argv[0]->string); + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant remove file"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void rename(string from, string to); + */ + +static int renameProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int rc; + + if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: rename(old, new)"); + return -1; + } + + unlink(argv[1]->string); + rc = rename(argv[0]->string, argv[1]->string); + if (rc < 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant rename file"); + return -1; + } + return 0; +} + +/******************************************************************************/ +/* + * function void copy(string old, string new); + */ + +static int copyProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFile *from, *to; + char *buf; + int bytes, rc; + + if (argc != 2 || !ejsVarIsString(argv[0]) || !ejsVarIsString(argv[1])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: copy(old, new)"); + return -1; + } + + buf = mprAlloc(ejs, MPR_BUFSIZE); + if (buf == 0) { + ejsMemoryError(ejs); + return -1; + } + + from = mprOpen(ejs, argv[0]->string, O_RDONLY | O_BINARY, 0664); + if (from == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant open %s", argv[0]->string); + mprFree(buf); + return -1; + } + + to = mprOpen(ejs, argv[1]->string, O_CREAT | O_BINARY, 0664); + if (to == 0) { + ejsError(ejs, EJS_IO_ERROR, "Cant create %s", argv[1]->string); + mprClose(from); + mprFree(buf); + return -1; + } + + rc = 0; + while ((bytes = mprRead(from, buf, MPR_BUFSIZE)) > 0) { + if (mprWrite(to, buf, bytes) != bytes) { + ejsError(ejs, EJS_IO_ERROR, "Write error to %s", argv[1]->string); + rc = -1; + break; + } + } + + mprClose(from); + mprClose(to); + mprFree(buf); + + return rc; +} + +/******************************************************************************/ +/* + * function FileInfo getFileInfo(string path); + * + * MOB -- should create a real class FileInfo + */ + +static int getFileInfoProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprFileInfo info; + EjsVar *fileInfo; + int rc; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: getFileInfo(path)"); + return -1; + } + + fileInfo = ejsCreateObjVar(ejs); + if (fileInfo == 0) { + ejsMemoryError(ejs); + return -1; + } + ejsMakeObjPermanent(fileInfo, 1); + + rc = mprGetFileInfo(ejs, argv[0]->string, &info); + if (rc < 0) { + ejsMakeObjPermanent(fileInfo, 0); + ejsFreeVar(ejs, fileInfo); + ejsError(ejs, EJS_IO_ERROR, "Cant get file info for %s", + argv[0]->string); + return -1; + } + + ejsSetPropertyToInteger(ejs, fileInfo, "created", info.ctime); + ejsSetPropertyToInteger(ejs, fileInfo, "length", info.size); + ejsSetPropertyToBoolean(ejs, fileInfo, "isDir", info.isDir); + + ejsSetReturnValue(ejs, fileInfo); + ejsMakeObjPermanent(fileInfo, 0); + + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineFileSystemClass(Ejs *ejs) +{ + EjsVar *fileSystemClass; + + fileSystemClass = ejsDefineClass(ejs, "FileSystem", "Object", 0); + if (fileSystemClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ejs, fileSystemClass, "access", accessProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "mkdir", mkdirProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "rmdir", rmdirProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "dirList", dirListProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "writeFile", writeFileProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "readFile", readFileProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "remove", removeProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "rename", renameProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "copy", copyProc, 0); + ejsDefineCMethod(ejs, fileSystemClass, "getFileInfo", getFileInfoProc, 0); + + // MOB -- should be a property with accessor + ejsDefineCMethod(ejs, fileSystemClass, "getFreeSpace", getFreeSpaceProc, 0); + + return ejsObjHasErrors(fileSystemClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsHTTP.c b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsHTTP.c new file mode 100755 index 0000000000..25821f6960 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/WIN/ejsHTTP.c @@ -0,0 +1,488 @@ +/* + * @file ejsHTTP.c + * @brief HTTP class for the EJ System Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +#if UNUSED +/*********************************** Defines **********************************/ + +#define EJS_WEB_PROPERTY "-web" +#define EJS_HTTP_PROPERTY "-http" + +#define EJS_HTTP_DISPOSED 550 + +/* + * Control structure for one HTTP request structure + */ +typedef struct HTTPControl { + Ejs *ejs; + IWebResp *webResp; + AEECallback *callback; + MprBuf *buf; + EjsVar *thisObj; + char *url; + MprTime requestStarted; + uint timeout; +} HTTPControl; + +/****************************** Forward Declarations **************************/ + +static void cleanup(HTTPControl *hp); +static int createWeb(Ejs *ejs, EjsVar *thisObj); +static void brewCallback(HTTPControl *hp); +static int httpDestructor(Ejs *ejs, EjsVar *vp); +static void httpCallback(HTTPControl *hp, int responseCode); +static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv); + +/******************************************************************************/ +/* + * Constructor + */ + +int ejsHTTPConstructor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 0 && argc != 2) { + ejsError(ejs, EJS_ARG_ERROR, + "Bad usage: HTTP([obj = this, method = onComplete]);"); + return -1; + } + + if (createWeb(ejs, thisObj) < 0) { + return -1; + } + + setCallback(ejs, thisObj, argc, argv); + return 0; +} + +/******************************************************************************/ + +static int createWeb(Ejs *ejs, EjsVar *thisObj) +{ + MprApp *app; + void *web; + + app = mprGetApp(ejs); + + /* + * Create one instance of IWeb for the entire application. Do it here + * so only widgets that require HTTP incurr the overhead. + */ + web = mprGetKeyValue(ejs, "bpWeb"); + if (web == 0) { + if (ISHELL_CreateInstance(app->shell, AEECLSID_WEB, &web) != SUCCESS) { + ejsError(ejs, EJS_IO_ERROR, "Can't create IWEB"); + return -1; + } + } + mprSetKeyValue(ejs, "bpWeb", web); + return 0; +} + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function setCallback(obj, methodString); + */ + +static int setCallback(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc >= 1) { + ejsSetProperty(ejs, thisObj, "obj", argv[0]); + } else { + ejsSetProperty(ejs, thisObj, "obj", thisObj); + } + + if (argc >= 2) { + ejsSetProperty(ejs, thisObj, "method", argv[1]); + } else { + ejsSetPropertyToString(ejs, thisObj, "method", "onComplete"); + } + + return 0; +} + +/******************************************************************************/ +/* + * function fetch(); + */ + +static int fetchProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + HTTPControl *hp; + EjsProperty *pp; + MprApp *app; + IWeb *web; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsError(ejs, EJS_ARG_ERROR, "Bad usage: fetch(url)"); + return -1; + } + + app = mprGetApp(ejs); + web = (IWeb*) mprGetKeyValue(ejs, "bpWeb"); + + /* + * Web options + * + * WEBOPT_USERAGENT (char*) sets user agent + * WEBOPT_HANDLERDATA (void*) + * WEBOPT_CONNECTTIMEOUT (uint) msec + * WEBOPT_CONTENTLENGTH (long) + * WEBOPT_IDLECONNTIMEOUT (int) + * WEBOPT_ACTIVEXACTIONST (uint) Number of active requests + * + * WEBREQUEST_REDIRECT redirect transparently + * + */ + + hp = mprAllocType(ejs, HTTPControl); + if (hp == 0) { + ejsMemoryError(ejs); + return -1; + } + + hp->ejs = ejs; + hp->buf = mprCreateBuf(hp, MPR_BUF_INCR, MPR_MAX_BUF); + if (hp->buf == 0) { + mprFree(hp); + ejsMemoryError(ejs); + return -1; + } + + /* + * We copy thisObj because we need to preserve both the var and the object. + * We pass the var to brewCallback and so it must persist. The call to + * ejsMakeObjPermanent will stop the GC from collecting the object. + */ + hp->thisObj = ejsDupVar(ejs, thisObj, EJS_SHALLOW_COPY); + ejsSetVarName(ejs, hp->thisObj, "internalHttp"); + + /* + * Must keep a reference to the http object + */ + ejsMakeObjPermanent(hp->thisObj, 1); + + /* + * Make a property so we can access the HTTPControl structure from other + * methods. + */ + pp = ejsSetPropertyToPtr(ejs, thisObj, EJS_HTTP_PROPERTY, hp, 0); + ejsMakePropertyEnumerable(pp, 0); + ejsSetObjDestructor(ejs, hp->thisObj, httpDestructor); + + hp->url = mprStrdup(hp, argv[0]->string); + + hp->timeout = ejsGetPropertyAsInteger(ejs, thisObj, "timeout"); + mprGetTime(hp, &hp->requestStarted); + + hp->callback = mprAllocTypeZeroed(hp, AEECallback); + CALLBACK_Init(hp->callback, brewCallback, hp); + + hp->webResp = 0; + IWEB_GetResponse(web, + (web, &hp->webResp, hp->callback, hp->url, + WEBOPT_HANDLERDATA, hp, + WEBOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)", + WEBOPT_CONNECTTIMEOUT, hp->timeout, + WEBOPT_COPYOPTS, TRUE, + WEBOPT_CONTENTLENGTH, 0, + WEBOPT_END)); + + ejsSetPropertyToString(ejs, thisObj, "status", "active"); + + return 0; +} + +/******************************************************************************/ +/* + * Called whenver the http object is deleted. + */ + +static int httpDestructor(Ejs *ejs, EjsVar *thisObj) +{ + HTTPControl *hp; + + /* + * If the httpCallback has run, then this property will not exist + */ + hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY); + + if (hp) { + cleanup(hp); + } + + return 0; +} + +/******************************************************************************/ +/* + * Stop the request immediately without calling the callback + */ + +static int stopProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + HTTPControl *hp; + + hp = ejsGetPropertyAsPtr(ejs, thisObj, EJS_HTTP_PROPERTY); + + if (hp) { + cleanup(hp); + } + + return 0; +} + +/******************************************************************************/ +/* + * Brew HTTP callback. Invoked for any return data. + */ + +static void brewCallback(HTTPControl *hp) +{ + Ejs *ejs; + EjsVar *thisObj; + ISource *source; + WebRespInfo *info; + char data[MPR_BUF_INCR]; + int bytes; + + mprAssert(hp); + mprAssert(hp->webResp); + + info = IWEBRESP_GetInfo(hp->webResp); + + if (info == 0) { + mprAssert(info); + /* should not happen */ + return; + } + + ejs = hp->ejs; + thisObj = hp->thisObj; + + if (! WEB_ERROR_SUCCEEDED(info->nCode)) { + ejsSetPropertyToString(ejs, thisObj, "status", "error"); + httpCallback(hp, info->nCode); + return; + } + + if (hp->timeout) { + if (mprGetTimeRemaining(hp, hp->requestStarted, hp->timeout) <= 0) { + ejsSetPropertyToString(ejs, thisObj, "status", "timeout"); + httpCallback(hp, 504); + return; + } + } + + /* + * Normal success + */ + source = info->pisMessage; + mprAssert(source); + + bytes = ISOURCE_Read(source, data, sizeof(data)); + + switch (bytes) { + case ISOURCE_WAIT: // No data yet + ISOURCE_Readable(source, hp->callback); + break; + + case ISOURCE_ERROR: + ejsSetPropertyToString(ejs, thisObj, "status", "error"); + httpCallback(hp, info->nCode); + break; + + case ISOURCE_END: + mprAddNullToBuf(hp->buf); + ejsSetPropertyToString(ejs, thisObj, "status", "complete"); + httpCallback(hp, info->nCode); + break; + + default: + if (bytes > 0) { + if (mprPutBlockToBuf(hp->buf, data, bytes) != bytes) { + ejsSetPropertyToString(ejs, thisObj, "status", "partialData"); + httpCallback(hp, 500); + } + } + ISOURCE_Readable(source, hp->callback); + break; + } +} + +/******************************************************************************/ +/* + * Invoke the HTTP completion method + */ + +static void httpCallback(HTTPControl *hp, int responseCode) +{ + Ejs *ejs; + EjsVar *thisObj, *callbackObj; + MprArray *args; + char *msg; + const char *callbackMethod; + + mprAssert(hp); + mprAssert(hp->webResp); + + thisObj = hp->thisObj; + ejs = hp->ejs; + + ejsSetPropertyToInteger(ejs, thisObj, "responseCode", responseCode); + if (mprGetBufLength(hp->buf) > 0) { + ejsSetPropertyToBinaryString(ejs, thisObj, "responseData", + mprGetBufStart(hp->buf), mprGetBufLength(hp->buf)); + } + + callbackObj = ejsGetPropertyAsVar(ejs, thisObj, "obj"); + callbackMethod = ejsGetPropertyAsString(ejs, thisObj, "method"); + + if (callbackObj != 0 && callbackMethod != 0) { + + args = mprCreateItemArray(ejs, EJS_INC_ARGS, EJS_MAX_ARGS); + mprAddItem(args, ejsDupVar(ejs, hp->thisObj, EJS_SHALLOW_COPY)); + + if (ejsRunMethod(ejs, callbackObj, callbackMethod, args) < 0) { + msg = ejsGetErrorMsg(ejs); + mprError(ejs, MPR_LOC, "HTTP callback failed. Details: %s", msg); + } + ejsFreeMethodArgs(ejs, args); + + } else if (ejsRunMethod(ejs, thisObj, "onComplete", 0) < 0) { + msg = ejsGetErrorMsg(ejs); + mprError(ejs, MPR_LOC, "HTTP onComplete failed. Details: %s", msg); + } + + cleanup(hp); +} + +/******************************************************************************/ +/* + * Cleanup + */ + +static void cleanup(HTTPControl *hp) +{ + Ejs *ejs; + MprApp *app; + int rc; + + mprAssert(hp); + mprAssert(hp->webResp); + + ejs = hp->ejs; + + if (hp->webResp) { + rc = IWEBRESP_Release(hp->webResp); + // mprAssert(rc == 0); + hp->webResp = 0; + } + + if (hp->callback) { + CALLBACK_Cancel(hp->callback); + mprFree(hp->callback); + hp->callback = 0; + } + + /* + * Once the property is deleted, then if the destructor runs, it will + * notice that the EJS_HTTP_PROPERTY is undefined. + */ + ejsDeleteProperty(ejs, hp->thisObj, EJS_HTTP_PROPERTY); + + /* + * Allow garbage collection to work on thisObj + */ + ejsMakeObjPermanent(hp->thisObj, 0); + ejsFreeVar(ejs, hp->thisObj); + + mprFree(hp->buf); + mprFree(hp->url); + + mprFree(hp); + + app = mprGetApp(ejs); + + + ISHELL_SendEvent(app->shell, (AEECLSID) app->classId, EVT_USER, 0, 0); +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineHTTPClass(Ejs *ejs) +{ + EjsVar *httpClass; + + httpClass = + ejsDefineClass(ejs, "HTTP", "Object", ejsHTTPConstructor); + if (httpClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the methods + */ + ejsDefineCMethod(ejs, httpClass, "fetch", fetchProc, 0); + ejsDefineCMethod(ejs, httpClass, "stop", stopProc, 0); + ejsDefineCMethod(ejs, httpClass, "setCallback", setCallback, 0); + +#if FUTURE + ejsDefineCMethod(ejs, httpClass, "put", put, 0); + ejsDefineCMethod(ejs, httpClass, "upload", upload, 0); + ejsDefineCMethod(ejs, httpClass, "addUploadFile", addUploadFile, 0); + ejsDefineCMethod(ejs, httpClass, "addPostData", addPostData, 0); + ejsDefineCMethod(ejs, httpClass, "setUserPassword", setUserPassword, 0); + ejsDefineCMethod(ejs, httpClass, "addCookie", addCookie, 0); +#endif + + /* + * Define properties + */ + ejsSetPropertyToString(ejs, httpClass, "status", "inactive"); + + /* This default should come from player.xml */ + + ejsSetPropertyToInteger(ejs, httpClass, "timeout", 30 * 1000); + ejsSetPropertyToInteger(ejs, httpClass, "responseCode", 0); + + return ejsObjHasErrors(httpClass) ? MPR_ERR_CANT_INITIALIZE: 0; +} + +/******************************************************************************/ + +void ejsTermHTTPClass(Ejs *ejs) +{ + IWeb *web; + int rc; + + web = (IWeb*) mprGetKeyValue(ejs, "bpWeb"); + if (web) { + rc = IWEB_Release(web); + mprAssert(rc == 0); + } +} + +#endif +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsGC.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsGC.c new file mode 100644 index 0000000000..411975f80e --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsGC.c @@ -0,0 +1,326 @@ +/* + * @file ejsGC.c + * @brief Garbage collector class for the EJS Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +#if (WIN || BREW_SIMULATOR) && BLD_DEBUG + +static int checkProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + _CrtCheckMemory(); + return 0; +} + +#endif +/******************************************************************************/ + +static int debugProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsError(ep, EJS_ARG_ERROR, "Bad args: debug(debugLevel)"); + return -1; + } + + ejsSetGCDebugLevel(ep, ejsVarToInteger(argv[0])); + return 0; +} + +/******************************************************************************/ +/* + * Print stats and dump objects + */ + +static int printStatsProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + bool leakStats; + + if (argc > 1) { + leakStats = ejsVarToInteger(argv[0]); + } else { + leakStats = 0; + } + +#if BLD_FEATURE_ALLOC_STATS + ejsPrintAllocReport(ep, 0); + + mprPrintAllocReport(mprGetApp(ep), leakStats, 0); +#endif + +#if BLD_DEBUG + ejsDumpObjects(ep); +#endif + + return 0; +} + +/******************************************************************************/ + +static int runProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc > 1) { + ejsError(ep, EJS_ARG_ERROR, "Bad args: run([quick])"); + return -1; + } + + if (argc == 1) { + ejsIncrementalCollectGarbage(ep); + } else { + ejsCollectGarbage(ep, -1); + } + return 0; +} + +/******************************************************************************/ + +static int usedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ep, ejsGetUsedMemory(ep)); + return 0; +} + +/******************************************************************************/ + +static int allocatedMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, + EjsVar **argv) +{ +#if BLD_FEATURE_ALLOC_STATS + ejsSetReturnValueToInteger(ep, ejsGetAllocatedMemory(ep)); +#endif + return 0; +} + +/******************************************************************************/ + +static int mprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ +#if BLD_FEATURE_ALLOC_STATS + ejsSetReturnValueToInteger(ep, mprGetAllocatedMemory(ep)); +#endif + return 0; +} + +/******************************************************************************/ + +static int peakMprMemoryProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ +#if BLD_FEATURE_ALLOC_STATS + ejsSetReturnValueToInteger(ep, mprGetPeakAllocatedMemory(ep)); +#endif + return 0; +} + +/******************************************************************************/ + +static int getDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ep, ep->gc.debugLevel); + return 0; +} + +/******************************************************************************/ + +static int setDebugLevel(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + ep->gc.debugLevel= ejsVarToInteger(argv[0]); + return 0; +} + +/******************************************************************************/ + +static int getEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToBoolean(ep, ep->gc.enable); + return 0; +} + +/******************************************************************************/ + +static int setEnable(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + ep->gc.enable= ejsVarToBoolean(argv[0]); + return 0; +} + +/******************************************************************************/ + +static int getDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToBoolean(ep, ep->gc.enableDemandCollect); + return 0; +} + +/******************************************************************************/ + +static int setDemandCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + ep->gc.enableDemandCollect = ejsVarToBoolean(argv[0]); + return 0; +} + +/******************************************************************************/ + +static int getIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToBoolean(ep, ep->gc.enableIdleCollect); + return 0; +} + +/******************************************************************************/ + +static int setIdleCollect(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + ep->gc.enableIdleCollect = ejsVarToBoolean(argv[0]); + return 0; +} + +/******************************************************************************/ + +static int getWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ep, ep->gc.workQuota); + return 0; +} + +/******************************************************************************/ + +static int setWorkQuota(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int quota; + + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + quota = ejsVarToInteger(argv[0]); + if (quota < EJS_GC_MIN_WORK_QUOTA && quota != 0) { + ejsArgError(ep, "Bad work quota"); + return -1; + } + + ep->gc.workQuota = quota; + return 0; +} + +/******************************************************************************/ + +static int getMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ep, ep->gc.maxMemory); + return 0; +} + +/******************************************************************************/ + +static int setMaxMemory(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int maxMemory; + + if (argc != 1) { + ejsArgError(ep, "Bad arguments"); + return -1; + } + maxMemory = ejsVarToInteger(argv[0]); + if (maxMemory < 0) { + ejsArgError(ep, "Bad maxMemory"); + return -1; + } + + ep->gc.maxMemory = maxMemory; + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineGCClass(Ejs *ep) +{ + EjsVar *gcClass; + int flags; + + flags = EJS_NO_LOCAL; + + /* + * NOTE: We create the GC class and define static methods on it. There + * is no object instance + */ + gcClass = ejsDefineClass(ep, "System.GC", "Object", 0); + if (gcClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * MOB -- convert these to properties with accessors when available + */ + ejsDefineCMethod(ep, gcClass, "printStats", printStatsProc, flags); + ejsDefineCMethod(ep, gcClass, "run", runProc, flags); + + ejsDefineCMethod(ep, gcClass, "getUsedMemory", usedMemoryProc, flags); + ejsDefineCMethod(ep, gcClass, "getAllocatedMemory", allocatedMemoryProc, + flags); + ejsDefineCMethod(ep, gcClass, "getMprMemory", mprMemoryProc, flags); + ejsDefineCMethod(ep, gcClass, "getPeakMprMemory", peakMprMemoryProc, flags); + ejsDefineCMethod(ep, gcClass, "debug", debugProc, flags); + +#if (WIN || BREW_SIMULATOR) && BLD_DEBUG + ejsDefineCMethod(ep, gcClass, "check", checkProc, flags); +#endif + + ejsDefineCAccessors(ep, gcClass, "debugLevel", + getDebugLevel, setDebugLevel, flags); + + ejsDefineCAccessors(ep, gcClass, "enable", + getEnable, setEnable, flags); + + ejsDefineCAccessors(ep, gcClass, "demandCollect", + getDemandCollect, setDemandCollect, flags); + + ejsDefineCAccessors(ep, gcClass, "idleCollect", + getIdleCollect, setIdleCollect, flags); + + ejsDefineCAccessors(ep, gcClass, "workQuota", + getWorkQuota, setWorkQuota, flags); + + ejsDefineCAccessors(ep, gcClass, "maxMemory", + getMaxMemory, setMaxMemory, flags); + + return ejsObjHasErrors(gcClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsGlobal.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsGlobal.c new file mode 100755 index 0000000000..6ab8f867e1 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsGlobal.c @@ -0,0 +1,785 @@ +/* + * @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; + } + mprSleep(ep, ejsVarToInteger(argv[0])); + return 0; +} + +/******************************************************************************/ +/* + * sort properties + * FUTURE -- should have option to sort object based on a given property value + * ascending or descending + * Usage: sort(object, order = ascending, property = 0); + */ + +static int sortProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + const char *property; + int error, order; + + error = 0; + property = 0; + + /* + * Default order is increasing + */ + order = 1; + + if (argc < 1 || argc > 3 || !ejsVarIsObject(argv[0])) { + error++; + } + + if (argc >= 2) { + order = ejsVarToInteger(argv[1]); + } + + /* + * If property is not defined, it sorts the properties in the object + */ + if (argc == 3) { + if (! ejsVarIsString(argv[2])) { + error++; + } else { + property = argv[2]->string; + } + } + + if (error) { + ejsError(ep, EJS_ARG_ERROR, "Usage: sort(object, [order], [property])"); + return -1; + } + ejsSortProperties(ep, argv[0], 0, property, order); + return 0; +} + +/******************************************************************************/ +/* + * Get a time mark + * MOB -- WARNING: this can overflow. OK on BREW, but other O/Ss it may have + * overflowed on the first call. It should be renamed. + * MOB -- replace with proper Date. + */ + +static int timeProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + MprTime now; + + mprGetTime(ep, &now); +#if WIN || LINUX || SOLARIS +{ + /* MOB -- poor hack */ + static MprTime initial; + if (initial.sec == 0) { + initial = now; + } + now.sec -= initial.sec; + + if (initial.msec > now.msec) { + now.msec = now.msec + 1000 - initial.msec; + now.sec--; + } else { + now.msec -= initial.msec; + } +} +#endif + /* MOB -- this can overflow */ + ejsSetReturnValueToInteger(ep, now.sec * 1000 + now.msec); + return 0; +} + +/******************************************************************************/ +/* + * MOB -- Temporary Get the date (time since Jan 6, 1980 GMT + */ + +static int dateProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ +#if BREW + uint now; + + now = GETTIMESECONDS(); + ejsSetReturnValueToInteger(ep, now); +#endif + return 0; +} + +/******************************************************************************/ +/* + * strlen(string) + */ + +static int strlenProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + char *buf; + int len; + + if (argc != 1) { + ejsError(ep, EJS_ARG_ERROR, "Usage: strlen(var)"); + return -1; + } + + len = 0; + if (! ejsVarIsString(argv[0])) { + buf = ejsVarToString(ep, argv[0]); + if (buf) { + len = strlen(buf); + } + + } else { + len = argv[0]->length; + } + + ejsSetReturnValueToInteger(ep, len); + return 0; +} + +/******************************************************************************/ +/* + * toint(num) + */ + +static int tointProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int i; + + if (argc != 1) { + ejsError(ep, EJS_ARG_ERROR, "Usage: toint(number)"); + return -1; + } + + i = ejsVarToInteger(argv[0]); + + ejsSetReturnValueToInteger(ep, i); + return 0; +} + +/******************************************************************************/ +/* + * string strstr(string, pat) + */ + +static int strstrProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + char *str, *pat; + char *s; + int strAlloc; + + if (argc != 2) { + ejsError(ep, EJS_ARG_ERROR, "Usage: strstr(string, pat)"); + return -1; + } + + str = ejsVarToString(ep, argv[0]); + + strAlloc = ep->castAlloc; + ep->castTemp = 0; + + pat = ejsVarToString(ep, argv[1]); + + s = strstr(str, pat); + + if (s == 0) { + ejsSetReturnValueToUndefined(ep); + } else { + ejsSetReturnValueToString(ep, s); + } + + if (strAlloc) { + mprFree(str); + } + + return 0; +} + +/******************************************************************************/ +/* + * Trace + */ + +static int traceProc(Ejs *ep, EjsVar *thisObj, int argc, char **argv) +{ + if (argc == 1) { + mprLog(ep, 0, "%s", argv[0]); + + } else if (argc == 2) { + mprLog(ep, atoi(argv[0]), "%s", argv[1]); + + } else { + ejsError(ep, EJS_ARG_ERROR, "Usage: trace([level], message)"); + return -1; + } + ejsWriteVarAsString(ep, ep->result, ""); + return 0; +} + +/******************************************************************************/ +/* + * Evaluate a sub-script. It is evaluated in the same variable scope as + * the calling script / method. + */ + +static int evalScriptProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + EjsVar *arg; + int i; + + ejsWriteVarAsUndefined(ep, ep->result); + + for (i = 0; i < argc; i++) { + arg = argv[i]; + if (arg->type != EJS_TYPE_STRING) { + continue; + } + if (ejsEvalScript(ep, arg->string, 0) < 0) { + return -1; + } + } + /* + * Return with the value of the last expression + */ + return 0; +} + +/******************************************************************************/ + +/* MOB -- need a real datatype returning int, int64, etc */ + +static int typeofProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + const struct { + EjsType type; + const char *name; + } types[] = { + { EJS_TYPE_UNDEFINED, "undefined" }, +#if EJS_ECMA_STND + { EJS_TYPE_NULL, "object" }, +#else + { EJS_TYPE_NULL, "null" }, +#endif + { EJS_TYPE_BOOL, "boolean" }, + { EJS_TYPE_CMETHOD, "function" }, + { EJS_TYPE_FLOAT, "number" }, + { EJS_TYPE_INT, "number" }, + { EJS_TYPE_INT64, "number" }, + { EJS_TYPE_OBJECT, "object" }, + { EJS_TYPE_METHOD, "function" }, + { EJS_TYPE_STRING, "string" }, + { EJS_TYPE_STRING_CMETHOD, "function" }, + { EJS_TYPE_PTR, "pointer" } + }; + const char *type; + int i; + + type = NULL; + if (argc != 1) { + ejsError(ep, EJS_ARG_ERROR, "Bad args: typeof(var)"); + return -1; + } + + for (i = 0; i < MPR_ARRAY_SIZE(types); i++) { + if (argv[0]->type == types[i].type) { + type = types[i].name; + break; + } + } + if (type == NULL) { + mprAssert(type); + return -1; + } + + ejsSetReturnValueToString(ep, type); + return 0; +} + +/******************************************************************************/ +/* + * Define the standard properties and methods inherited by all interpreters + * Obj is set to the global class in the default interpreter. When an + * interpreter attempts to write to any property, a copy will be written + * into the interpeters own global space. This is like a "copy-on-write". + */ + +int ejsDefineGlobalProperties(Ejs *ep) +{ + EjsVar *obj; + + obj = ep->service->globalClass; + mprAssert(obj); + + ejsSetPropertyToNull(ep, obj, "null"); + ejsSetPropertyToUndefined(ep, obj, "undefined"); + ejsSetPropertyToBoolean(ep, obj, "true", 1); + ejsSetPropertyToBoolean(ep, obj, "false", 0); + +#if BLD_FEATURE_FLOATING_POINT + { + /* MOB. Fix. This generates warnings on some systems. + This is intended. */ + double d = 0.0; + double e = 0.0; + ejsSetPropertyToFloat(ep, obj, "NaN", e / d); + + d = MAX_FLOAT; + ejsSetPropertyToFloat(ep, obj, "Infinity", d * d); + } +#endif + +#if BLD_FEATURE_LEGACY_API + /* + * DEPRECATED: 2.0. + * So that ESP/ASP can ignore "language=javascript" statements + */ + ejsSetPropertyToInteger(ep, obj, "javascript", 0); +#endif + + /* + * Extension methods. We go directly to the mpr property APIs for speed. + * Flags will cause the callbacks to be supplied the Ejs handle. + */ + ejsDefineCMethod(ep, obj, "assert", assertProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "breakpoint", breakpointProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "basename", basenameProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "dirname", dirnameProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "stripext", stripextProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "trim", trimProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "eval", evalScriptProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "exit", exitScript, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "print", printProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "println", printlnProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "sleep", sleepProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "sort", sortProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "time", timeProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "date", dateProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "strlen", strlenProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "strstr", strstrProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "typeof", typeofProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "toint", tointProc, EJS_NO_LOCAL); + + ejsDefineStringCMethod(ep, obj, "include", includeProc, EJS_NO_LOCAL); + ejsDefineStringCMethod(ep, obj, "includeGlobal", includeGlobalProc, + EJS_NO_LOCAL); + ejsDefineStringCMethod(ep, obj, "trace", traceProc, EJS_NO_LOCAL); + +#if BLD_DEBUG + ejsDefineCMethod(ep, obj, "printv", printvProc, EJS_NO_LOCAL); +#endif + +#if FUTURE + ejsDefineCMethod(ep, obj, "printf", printfProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, obj, "sprintf", sprintfProc, EJS_NO_LOCAL); +#endif + + if (ejsObjHasErrors(obj)) { + return MPR_ERR_CANT_INITIALIZE; + } + return 0; +} + +/******************************************************************************/ + +#else +void ejsProcsDummy() {} + +/******************************************************************************/ +#endif /* BLD_FEATURE_EJS */ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystem.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystem.c new file mode 100644 index 0000000000..e035e1c740 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystem.c @@ -0,0 +1,112 @@ +/* + * @file ejsSystem.c + * @brief System class for the EJS Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +#if UNUSED +/* + * function int random() + */ + +static int randomProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "random()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function void yield() + */ + +static int yieldProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "yield()\n"); + return 0; +} + +/******************************************************************************/ +/* + * function void sleep(int milliSeconds) + */ + +static int sleepProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ep, "sleep()\n"); + return 0; +} + +#endif +/******************************************************************************/ +/* + * function void exit(int status) + * + * Exit the widget with the given status. All JavaScript processing ceases. + */ + +static int exitProc(Ejs *ep, EjsVar *thisObj, int argc, EjsVar **argv) +{ + int status; + + status = 0; + if ((argc == 1) && ejsVarIsInteger(argv[0])) { + status = argv[0]->integer; + } + ejsExit(ep, status); + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineSystemClass(Ejs *ep) +{ + EjsVar *systemClass; + + /* + * We create the system class and define static methods on it. + * NOTE: There is no object instance + */ + systemClass = ejsDefineClass(ep, "System", "Object", 0); + if (systemClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + ejsDefineCMethod(ep, systemClass, "exit", exitProc, EJS_NO_LOCAL); + +#if UNUSED + ejsDefineCMethod(ep, systemClass, "random", randomProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, systemClass, "yield", yieldProc, EJS_NO_LOCAL); + ejsDefineCMethod(ep, systemClass, "sleep", sleepProc, EJS_NO_LOCAL); + + /* + * Define properties + */ + ejsSetPropertyToString(systemClass, "name", ""); +#endif + + return ejsObjHasErrors(systemClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemApp.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemApp.c new file mode 100644 index 0000000000..e2f1ceb363 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemApp.c @@ -0,0 +1,49 @@ +/* + * @file ejsSystemApp.c + * @brief App class + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software Inc, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/************************************ Code ************************************/ + +int ejsDefineAppClass(Ejs *ep) +{ + EjsVar *appClass; + + appClass = ejsDefineClass(ep, "System.App", "Object", 0); + if (appClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define properties + */ + ejsSetPropertyToString(ep, appClass, "name", BLD_PRODUCT); + ejsSetPropertyToString(ep, appClass, "title", BLD_NAME); + ejsSetPropertyToString(ep, appClass, "version", BLD_VERSION); + + /* + * Command line arguments + */ + ejsSetPropertyToNull(ep, appClass, "args"); + + return ejsObjHasErrors(appClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemDebug.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemDebug.c new file mode 100644 index 0000000000..5a011e2a2d --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemDebug.c @@ -0,0 +1,60 @@ +/* + * @file ejsSystemDebug.c + * @brief System.Debug class + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function bool isDebugMode() + * MOB -- convert to accessor + */ + +static int isDebugMode(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsTrace(ejs, "isDebugMode()\n"); + ejsSetReturnValueToInteger(ejs, mprGetDebugMode(ejs)); + return 0; +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineDebugClass(Ejs *ejs) +{ + EjsVar *systemDebugClass; + + systemDebugClass = ejsDefineClass(ejs, "System.Debug", "Object", 0); + if (systemDebugClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the class methods + */ + ejsDefineCMethod(ejs, systemDebugClass, "isDebugMode", isDebugMode, + EJS_NO_LOCAL); + + return ejsObjHasErrors(systemDebugClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemLog.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemLog.c new file mode 100644 index 0000000000..66467f8fcf --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemLog.c @@ -0,0 +1,163 @@ +/* + * @file ejsSystemLog.c + * @brief System.Log class for the EJS Object Model + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/*********************************** Usage ************************************/ +/* + * System.Log.setLog(path); + * System.Log.enable; + */ +/******************************************************************************/ + +static void logHandler(MPR_LOC_DEC(ctx, loc), int flags, int level, + const char *msg) +{ + MprApp *app; + char *buf; + int len; + + app = mprGetApp(ctx); + if (app->logFile == 0) { + return; + } + + if (flags & MPR_LOG_SRC) { + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "Log %d: %s\n", level, msg); + + } else if (flags & MPR_ERROR_SRC) { + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "Error: %s\n", msg); + + } else if (flags & MPR_FATAL_SRC) { + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "Fatal: %s\n", msg); + + } else if (flags & MPR_ASSERT_SRC) { +#if BLD_FEATURE_ALLOC_LEAK_TRACK + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "Assertion %s, failed at %s\n", + msg, loc); +#else + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "Assertion %s, failed\n", msg); +#endif + + } else if (flags & MPR_RAW) { + /* OPT */ + len = mprAllocSprintf(MPR_LOC_PASS(ctx, loc), &buf, 0, + "%s", msg); + + } else { + return; + } + + mprPuts(app->logFile, buf, len); + + mprFree(buf); +} + +/******************************************************************************/ +/************************************ Methods *********************************/ +/******************************************************************************/ +/* + * function int setLog(string path) + */ + +static int setLog(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + const char *path; + MprFile *file; + MprApp *app; + + if (argc != 1 || !ejsVarIsString(argv[0])) { + ejsArgError(ejs, "Usage: setLog(path)"); + return -1; + } + + app = mprGetApp(ejs); + + /* + * Ignore errors if we can't create the log file. + * Use the app context so this will live longer than the interpreter + * MOB -- this leaks files. + */ + path = argv[0]->string; + file = mprOpen(app, path, O_CREAT | O_TRUNC | O_WRONLY, 0664); + if (file) { + app->logFile = file; + mprSetLogHandler(ejs, logHandler); + } + mprLog(ejs, 0, "Test log"); + + return 0; +} + +/******************************************************************************/ +#if UNUSED + +static int enableSetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + if (argc != 1) { + ejsArgError(ejs, "Usage: set(value)"); + return -1; + } + ejsSetProperty(ejs, thisObj, "_enabled", argv[0]); + return 0; +} + +/******************************************************************************/ + +static int enableGetAccessor(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValue(ejs, ejsGetPropertyAsVar(ejs, thisObj, "_enabled")); + return 0; +} + +#endif +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineLogClass(Ejs *ejs) +{ + EjsVar *logClass; + + logClass = ejsDefineClass(ejs, "System.Log", "Object", 0); + if (logClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + ejsDefineCMethod(ejs, logClass, "setLog", setLog, EJS_NO_LOCAL); + +#if UNUSED + EjsProperty *pp; + ejsDefineCAccessors(ejs, logClass, "enable", enableSetAccessor, + enableGetAccessor, EJS_NO_LOCAL); + + pp = ejsSetPropertyToBoolean(ejs, logClass, "_enabled", 0); + ejsMakePropertyEnumerable(pp, 0); +#endif + + return ejsObjHasErrors(logClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ diff --git a/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemMemory.c b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemMemory.c new file mode 100755 index 0000000000..d10787b1b4 --- /dev/null +++ b/source4/lib/appweb/ejs-2.0/ejs/system/ejsSystemMemory.c @@ -0,0 +1,174 @@ +/* + * @file ejsSystemMemory.c + * @brief System.Memory class + */ +/********************************** Copyright *********************************/ +/* + * Copyright (c) Mbedthis Software LLC, 2005-2006. All Rights Reserved. + */ +/********************************** Includes **********************************/ + +#include "ejs.h" + +/****************************** Forward Declarations***************************/ + +static uint getUsedMemory(Ejs *ejs); + +/******************************************************************************/ +/*********************************** Methods *********************************/ +/******************************************************************************/ + +static int getUsedMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ejs, getUsedMemory(ejs)); + return 0; +} + +/******************************************************************************/ + +static int getUsedStackProc(Ejs *ejs, EjsVar *thisObj, int argc, EjsVar **argv) +{ + ejsSetReturnValueToInteger(ejs, mprStackSize(ejs)); + return 0; +} + +/******************************************************************************/ +/* + * Public function + */ + +uint ejsGetAvailableMemory(Ejs *ejs) +{ + EjsVar *memoryClass; + uint ram; + + memoryClass = ejsGetClass(ejs, 0, "System.Memory"); + + ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram"); + return ram - getUsedMemory(ejs); +} + +/******************************************************************************/ + +static int getAvailableMemoryProc(Ejs *ejs, EjsVar *thisObj, int argc, + EjsVar **argv) +{ + EjsVar *memoryClass; + uint ram; + + memoryClass = ejsGetClass(ejs, 0, "System.Memory"); + + ram = ejsGetPropertyAsInteger(ejs, memoryClass, "ram"); +#if BREW + ejsSetReturnValueToInteger(ejs, ram - getUsedMemory(ejs)); +#else + ejsSetReturnValueToInteger(ejs, 0); +#endif + return 0; +} + +/******************************************************************************/ + +static uint getUsedMemory(Ejs *ejs) +{ +#if BREW + MprApp *app; + IHeap *heap; + uint memInUse; + void *ptr; + + app = mprGetApp(ejs); + ptr = (void*) &heap; + if (ISHELL_CreateInstance(app->shell, AEECLSID_HEAP, (void**) ptr) + == SUCCESS) { + memInUse = IHEAP_GetMemStats(heap); + IHEAP_Release(heap); + } else { + memInUse = 0; + } + + return memInUse; +#else + return 0; +#endif +} + +/******************************************************************************/ +/******************************** Initialization ******************************/ +/******************************************************************************/ + +int ejsDefineMemoryClass(Ejs *ejs) +{ + EjsVar *memoryClass; + uint used; + +#if BREW + MprApp *app; + AEEDeviceInfo *info; + + /* + * Get needed information for class properties. + */ + info = mprAllocType(ejs, AEEDeviceInfo); + if (info == 0) { + return MPR_ERR_CANT_ALLOCATE; + } + info->wStructSize = sizeof(AEEDeviceInfo); + app = mprGetApp(ejs); + ISHELL_GetDeviceInfo(app->shell, info); + used = getUsedMemory(ejs); +#else + used = 0; +#endif + + /* + * Create the class + */ + memoryClass = ejsDefineClass(ejs, "System.Memory", "Object", 0); + if (memoryClass == 0) { + return MPR_ERR_CANT_INITIALIZE; + } + + /* + * Define the class methods + * MOB -- change to accessors + */ + ejsDefineCMethod(ejs, memoryClass, "getUsedStack", getUsedStackProc, + EJS_NO_LOCAL); + ejsDefineCMethod(ejs, memoryClass, "getUsedMemory", getUsedMemoryProc, + EJS_NO_LOCAL); + ejsDefineCMethod(ejs, memoryClass, "getAvailableMemory", + getAvailableMemoryProc, EJS_NO_LOCAL); + + /* + * Define properties + */ +#if BREW + ejsSetPropertyToInteger(ejs, memoryClass, "ram", info->dwRAM); + +#if UNUSED + /* MOB -- delete this */ + ejsSetPropertyToInteger(ejs, memoryClass, "available", + info->dwRAM - used); +#endif +#endif + +#if UNUSED + ejsSetPropertyToInteger(ejs, memoryClass, "used", used); + ejsSetPropertyToInteger(ejs, memoryClass, "flash", 0); +#endif + + return ejsObjHasErrors(memoryClass) ? MPR_ERR_CANT_INITIALIZE : 0; +} + +/******************************************************************************/ + +/* + * Local variables: + * tab-width: 4 + * c-basic-offset: 4 + * End: + * vim:tw=78 + * vim600: sw=4 ts=4 fdm=marker + * vim<600: sw=4 ts=4 + */ |