#include #include #include #include #include #include #include #include #include #include /* File object - file.c Provides access to information about a file. class File { public: //Get the file permissions int getPermissions() //Is the file readable. bool isReadable() //Is the file writable bool isWritable() //Is the file executable bool isExecutable() //Is the file a link bool isLink() //Is the file a pipe bool isPipe() //Is the file a socket bool isSocket() //Is this a directory bool isDirectory() //Get the last modification date Date getLastModified() //Get the last access time Date getLastAccess() //Get the creation time Date getCreationTime() //Get file size int getSize() //Truncate the file to a given length void truncate(int size) } Stream object Abstract object that is intended to provide a base API for streams, be they a socket, a file, a directory, or whatever. class Stream { public: //Read a line string read() //Read len bytes string read(int len) //Read a single character string readChar() //Seek to a stream position void seek(int pos, int base_point) //Get the number of bytes available for reading, if available. int getBytesAvailable() //Write data to stream int write(string data) //Write data to stream, only write len bytes int write(string data, int len) //Determine if the stream is open bool isOpen() //Determine if we are at the end of the stream bool isEnd() //Get the error code from the last operation. int getLastError() } FileStream object Provides access to a stream for reading and writing to files. class FileStream extends Stream { public: } Directory object Allows creation/deletion of a directory. Information about a directory can be obtained from the file object. class Directory extends File { public: //Get number of files and directories in the directory. int getSize() //Get number of files int getTotalFiles() //Get number of directories int getTotalDirectories() //Unsupported, throw exception. void truncate(int size) } DirectoryStream object Allows enumeration of directory contents. class DirectoryStream extends FileStream { public: //Read a filename string read() //Read len number of filenames string read(int len) //Unsupported, throw exception. string readChar() //Unsupported, throw exception. void seek(int pos, int base_point) //Gets number of filenames available for reading. int getBytesAvailable() //Unsupported, throw exception. int write(string data) //Unsupported, throw exception. int write(string data, int len) } */ /* Function Prototypes */ JSBool js_engine_execute_file(JSContext *ctx, const char *file); static void js_error_handler(JSContext *ctx, const char *msg, JSErrorReport *er); static char *staterrorstr(int e); static jsval printf_exception(JSContext *cx, char *msg, ...); static size_t JSString_to_CString(JSString *str, char **msg); JSClass js_global_object_class = { "System", 0, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_PropertyStub, JS_EnumerateStub, JS_ResolveStub, JS_ConvertStub, JS_FinalizeStub, JSCLASS_NO_OPTIONAL_MEMBERS }; int main(int argc, char **argv){ char *fileToRun=NULL; JSRuntime *rt=NULL; JSContext *cx=NULL; JSObject *globalObj=NULL; JSObject *obj=NULL; if (argc > 1){ fileToRun = argv[1]; } rt = JS_NewRuntime(8L*1024L); if (!rt){ printf("Failed to initialize JS Runtime.\n"); return 1; } cx = JS_NewContext(rt, 8L*1024L*1024L); if (!cx){ printf("Failed to initialize JS Context.\n"); JS_DestroyRuntime(rt); return 1; } JS_SetErrorReporter(cx, js_error_handler); globalObj = JS_NewObject(cx, &js_global_object_class, NULL, NULL); if (!globalObj){ printf("Failed to create global object.\n"); JS_DestroyContext(cx); JS_DestroyRuntime(rt); return 1; } JS_InitStandardClasses(cx, globalObj); /* Define our new object. */ register_class_Console(cx); register_class_File(cx); register_class_Directory(cx); if (fileToRun){ if (js_engine_execute_file(cx, fileToRun)){ printf("File %s has been successfully executed.\n", fileToRun); } else { printf("Failed to executed %s.\n", fileToRun); } } else { printf("Usage: %s file\n", argv[0]); } JS_DestroyContext(cx); JS_DestroyRuntime(rt); return 0; } JSBool js_engine_execute_file(JSContext *ctx, const char *file){ JSScript *script; jsval returnValue; JSBool returnVal; JSObject *global = JS_GetGlobalObject(ctx); struct stat statinfo; if (file == NULL){ return JS_FALSE; } if (stat(file, &statinfo) == -1){ return JS_FALSE; } if (!S_ISREG(statinfo.st_mode)){ return JS_FALSE; } script = JS_CompileFile(ctx, global, file); if (script == NULL){ return JS_FALSE; } returnVal = JS_ExecuteScript(ctx, global, script, &returnValue); JS_DestroyScript(ctx, script); return returnVal; } static void js_error_handler(JSContext *ctx, const char *msg, JSErrorReport *er){ char *pointer=NULL; char *line=NULL; int len; if (er->linebuf != NULL){ len = er->tokenptr - er->linebuf + 1; pointer = malloc(len); memset(pointer, '-', len); pointer[len-1]='\0'; pointer[len-2]='^'; len = strlen(er->linebuf); line = malloc(len); strncpy(line, er->linebuf, len); } else { len=0; pointer = malloc(1); line = malloc(1); pointer[0]='\0'; line[0] = '\0'; } while (len > 0 && (line[len-1] == '\r' || line[len-1] == '\n')){ line[len-1]='\0'; len--; } printf("JS Error: %s\nFile: %s:%u\n", msg, er->filename, er->lineno); printf("%s\n%s\n", line, pointer); free(pointer); free(line); }