static void ProcessFile(JSContext *cx, JSObject *obj, const char *filename, FILE *file, JSBool forceTTY) { JSScript *script; jsval result; int lineno, startline; JSBool ok, hitEOF; char *bufp, buffer[4096]; JSString *str; if (forceTTY) { file = stdin; } else if (!isatty(fileno(file))) { /* * It's not interactive - just execute it. * * Support the UNIX #! shell hack; gobble the first line if it starts * with '#'. TODO - this isn't quite compatible with sharp variables, * as a legal js program (using sharp variables) might start with '#'. * But that would require multi-character lookahead. */ int ch = fgetc(file); if (ch == '#') { while((ch = fgetc(file)) != EOF) { if(ch == '\n' || ch == '\r') break; } } ungetc(ch, file); DoBeginRequest(cx); script = JS_CompileFileHandleForPrincipals(cx, obj, filename, file, gJSPrincipals); if (script) { if (!compileOnly) (void)JS_ExecuteScript(cx, obj, script, &result); JS_DestroyScript(cx, script); } DoEndRequest(cx); return; } /* It's an interactive filehandle; drop into read-eval-print loop. */ lineno = 1; hitEOF = JS_FALSE; do { bufp = buffer; *bufp = '\0'; /* * Accumulate lines until we get a 'compilable unit' - one that either * generates an error (before running out of source) or that compiles * cleanly. This should be whenever we get a complete statement that * coincides with the end of a line. */ startline = lineno; do { if (!GetLine(cx, bufp, file, startline == lineno ? "js> " : "")) { hitEOF = JS_TRUE; break; } bufp += strlen(bufp); lineno++; } while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer))); DoBeginRequest(cx); /* Clear any pending exception from previous failed compiles. */ JS_ClearPendingException(cx); script = JS_CompileScriptForPrincipals(cx, obj, gJSPrincipals, buffer, strlen(buffer), "typein", startline); if (script) { JSErrorReporter older; if (!compileOnly) { ok = JS_ExecuteScript(cx, obj, script, &result); if (ok && result != JSVAL_VOID) { /* Suppress error reports from JS_ValueToString(). */ older = JS_SetErrorReporter(cx, NULL); str = JS_ValueToString(cx, result); JS_SetErrorReporter(cx, older); if (str) fprintf(gOutFile, "%s\n", JS_GetStringBytes(str)); else ok = JS_FALSE; } } JS_DestroyScript(cx, script); } DoEndRequest(cx); } while (!hitEOF && !gQuitting); fprintf(gOutFile, "\n"); }
static void Process(JSContext *cx, JSObject *obj, char *filename, JSBool forceTTY) { SG_context * pCtx = NULL; char * sz = NULL; JSBool ok, hitEOF; JSObject *script; jsval result; JSString *str; char buffer[4096]; char *bufp; int lineno; int startline; SetContextOptions(cx); if (!forceTTY && filename != NULL && strlen(filename) != 0) { script = JS_CompileFile(cx, obj, filename); if (script) { if (!compileOnly) (void)JS_ExecuteScript(cx, obj, script, &result); } return; } (void)SG_context__alloc(&pCtx); /* It's an interactive filehandle; drop into read-eval-print loop. */ lineno = 1; hitEOF = JS_FALSE; do { bufp = buffer; *bufp = '\0'; /* * Accumulate lines until we get a 'compilable unit' - one that either * generates an error (before running out of source) or that compiles * cleanly. This should be whenever we get a complete statement that * coincides with the end of a line. */ startline = lineno; do { if (!GetLine(pCtx, cx, bufp, (SG_uint32)(sizeof(buffer)-(bufp-buffer)), stdin, startline == lineno ? "vscript> " : "")) { hitEOF = JS_TRUE; break; } bufp += strlen(bufp); lineno++; } while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer))); /* Clear any pending exception from previous failed compiles. */ JS_ClearPendingException(cx); script = JS_CompileScript(cx, obj, buffer, strlen(buffer), "typein", startline); if (script) { if (!compileOnly) { ok = JS_ExecuteScript(cx, obj, script, &result); if (ok && !JSVAL_IS_VOID(result)) { str = JS_ValueToString(cx, result); if (str) { SG_ERR_IGNORE( sg_jsglue__jsstring_to_sz(pCtx, cx, str, &sz) ); fprintf(gOutFile, "%s\n", sz); SG_NULLFREE(pCtx, sz); } } } } } while (!hitEOF && !gQuitting); SG_CONTEXT_NULLFREE(pCtx); fprintf(gOutFile, "\n"); return; }
static JSBool vm_isCompilableUnit(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval) { JSString *str; jsrefcount depth; size_t len, maxLen = 0; char *buf; size_t i; /** Coerce all args .toString() and calculate maximal buffer size */ for (i = 0; i < argc; i++) { if (!JSVAL_IS_STRING(argv[i])) { str = JS_ValueToString(cx, argv[i]); argv[i] = STRING_TO_JSVAL(str); } else str = JSVAL_TO_STRING(argv[i]); len = JS_GetStringLength(JSVAL_TO_STRING(argv[i])); if (len > maxLen) maxLen = len; } buf = JS_malloc(cx, maxLen + 1); if (!buf) return JS_FALSE; if (argc > 1) { for (i = 0; i < (argc - 1); i++) { str = JS_ValueToString(cx, argv[i]); if (!str) { JS_free(cx, buf); return JS_FALSE; } strcpy(buf, JS_GetStringBytes(str)); depth = JS_SuspendRequest(cx); gpsee_printf(cx, "%s%s", i ? " " : "", buf); JS_ResumeRequest(cx, depth); } } else i = 0; if (argc) { str = JS_ValueToString(cx, argv[i]); if (!str) { JS_free(cx, buf); return JS_FALSE; } strcpy(buf, JS_GetStringBytes(str)); } else *buf = (char)0; *rval = JS_BufferIsCompilableUnit(cx, obj, buf, strlen(buf)) == JS_TRUE ? JSVAL_TRUE : JSVAL_FALSE; JS_free(cx, buf); return JS_TRUE; }
NS_IMETHODIMP nsJSSh::IsBufferCompilable(PRBool *_retval) { JSAutoRequest ar(mJSContext); *_retval = JS_BufferIsCompilableUnit(mJSContext, mContextObj, mBuffer, mBufferPtr); return NS_OK; }
JSBool gjs_console_interact(JSContext *context, uintN argc, jsval *vp) { JSObject *object = JS_THIS_OBJECT(context, vp); gboolean eof = FALSE; JSObject *script = NULL; jsval result; JSString *str; GString *buffer = NULL; char *temp_buf = NULL; gunichar2 *u16_buffer; glong u16_buffer_len; int lineno; int startline; GError *error = NULL; FILE *file = stdin; JS_SetErrorReporter(context, gjs_console_error_reporter); /* It's an interactive filehandle; drop into read-eval-print loop. */ lineno = 1; do { /* * Accumulate lines until we get a 'compilable unit' - one that either * generates an error (before running out of source) or that compiles * cleanly. This should be whenever we get a complete statement that * coincides with the end of a line. */ startline = lineno; buffer = g_string_new(""); do { if (!gjs_console_readline(context, &temp_buf, file, startline == lineno ? "gjs> " : ".... ")) { eof = JS_TRUE; break; } g_string_append(buffer, temp_buf); g_free(temp_buf); lineno++; /* Note in this case, we are trying to parse the buffer as * ISO-8859-1 which is broken for non-ASCII. */ } while (!JS_BufferIsCompilableUnit(context, object, buffer->str, buffer->len)); if ((u16_buffer = g_utf8_to_utf16 (buffer->str, buffer->len, NULL, &u16_buffer_len, &error)) == NULL) { g_printerr ("%s\n", error->message); g_clear_error (&error); continue; } script = JS_CompileUCScript(context, object, u16_buffer, u16_buffer_len, "typein", startline); g_free (u16_buffer); if (script) JS_ExecuteScript(context, object, script, &result); if (JS_GetPendingException(context, &result)) { str = JS_ValueToString(context, result); JS_ClearPendingException(context); } else if (JSVAL_IS_VOID(result)) { goto next; } else { str = JS_ValueToString(context, result); } if (str) { char *display_str; display_str = gjs_value_debug_string(context, result); if (display_str != NULL) { g_fprintf(stdout, "%s\n", display_str); g_free(display_str); } } next: g_string_free(buffer, TRUE); } while (!eof); g_fprintf(stdout, "\n"); if (file != stdin) fclose(file); return JS_TRUE; }
JSBool gjs_console_interact(JSContext *context, unsigned argc, jsval *vp) { JSObject *object = JS_THIS_OBJECT(context, vp); gboolean eof = FALSE; jsval result; JSString *str; GString *buffer = NULL; char *temp_buf = NULL; int lineno; int startline; FILE *file = stdin; JS_SetErrorReporter(context, gjs_console_error_reporter); /* It's an interactive filehandle; drop into read-eval-print loop. */ lineno = 1; do { /* * Accumulate lines until we get a 'compilable unit' - one that either * generates an error (before running out of source) or that compiles * cleanly. This should be whenever we get a complete statement that * coincides with the end of a line. */ startline = lineno; buffer = g_string_new(""); do { if (!gjs_console_readline(context, &temp_buf, file, startline == lineno ? "cjs> " : ".... ")) { eof = JS_TRUE; break; } g_string_append(buffer, temp_buf); g_free(temp_buf); lineno++; } while (!JS_BufferIsCompilableUnit(context, object, buffer->str, buffer->len)); JS::CompileOptions options(context); options.setUTF8(true) .setFileAndLine("typein", startline); js::RootedObject rootedObj(context, object); JS::Evaluate(context, rootedObj, options, buffer->str, buffer->len, &result); gjs_schedule_gc_if_needed(context); if (JS_GetPendingException(context, &result)) { str = JS_ValueToString(context, result); JS_ClearPendingException(context); } else if (JSVAL_IS_VOID(result)) { goto next; } else { str = JS_ValueToString(context, result); } if (str) { char *display_str; display_str = gjs_value_debug_string(context, result); if (display_str != NULL) { g_fprintf(stdout, "%s\n", display_str); g_free(display_str); } } next: g_string_free(buffer, TRUE); } while (!eof); g_fprintf(stdout, "\n"); if (file != stdin) fclose(file); return JS_TRUE; }