2000-01-11 00:22:43 +03:00
|
|
|
// -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
//
|
|
|
|
// The contents of this file are subject to the Netscape Public
|
|
|
|
// License Version 1.1 (the "License"); you may not use this file
|
|
|
|
// except in compliance with the License. You may obtain a copy of
|
|
|
|
// the License at http://www.mozilla.org/NPL/
|
|
|
|
//
|
|
|
|
// Software distributed under the License is distributed on an "AS
|
|
|
|
// IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
|
|
|
|
// implied. See the License for the specific language governing
|
|
|
|
// rights and limitations under the License.
|
|
|
|
//
|
|
|
|
// The Original Code is the JavaScript 2 Prototype.
|
|
|
|
//
|
|
|
|
// The Initial Developer of the Original Code is Netscape
|
|
|
|
// Communications Corporation. Portions created by Netscape are
|
|
|
|
// Copyright (C) 1998 Netscape Communications Corporation. All
|
|
|
|
// Rights Reserved.
|
|
|
|
|
|
|
|
|
|
|
|
//
|
|
|
|
// JS2 shell.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include <algorithm>
|
2000-04-06 06:59:16 +04:00
|
|
|
|
2000-01-11 00:22:43 +03:00
|
|
|
#include "world.h"
|
2000-04-06 06:59:16 +04:00
|
|
|
#include "interpreter.h"
|
|
|
|
|
2000-01-11 00:22:43 +03:00
|
|
|
namespace JS = JavaScript;
|
|
|
|
using namespace JavaScript;
|
|
|
|
|
|
|
|
|
2000-04-05 08:24:19 +04:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#ifdef XP_MAC_MPW
|
|
|
|
/* Macintosh MPW replacements for the ANSI routines. These translate LF's to CR's because
|
|
|
|
the MPW libraries supplied by Metrowerks don't do that for some reason. */
|
|
|
|
static void translateLFtoCR(char *str, int length)
|
|
|
|
{
|
|
|
|
char *limit = str + length;
|
|
|
|
while (str != limit) {
|
|
|
|
if (*str == '\n')
|
|
|
|
*str = '\r';
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int fputc(int c, FILE *file)
|
|
|
|
{
|
|
|
|
char buffer = c;
|
|
|
|
if (buffer == '\n')
|
|
|
|
buffer = '\r';
|
|
|
|
return fwrite(&buffer, 1, 1, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fputs(const char *s, FILE *file)
|
|
|
|
{
|
|
|
|
char buffer[4096];
|
|
|
|
int n = strlen(s);
|
|
|
|
int extra = 0;
|
|
|
|
|
|
|
|
while (n > sizeof buffer) {
|
|
|
|
memcpy(buffer, s, sizeof buffer);
|
|
|
|
translateLFtoCR(buffer, sizeof buffer);
|
|
|
|
extra += fwrite(buffer, 1, sizeof buffer, file);
|
|
|
|
n -= sizeof buffer;
|
|
|
|
s += sizeof buffer;
|
|
|
|
}
|
|
|
|
memcpy(buffer, s, n);
|
|
|
|
translateLFtoCR(buffer, n);
|
|
|
|
return extra + fwrite(buffer, 1, n, file);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fprintf(FILE* file, const char *format, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
char smallBuffer[4096];
|
|
|
|
int n;
|
|
|
|
int bufferSize = sizeof smallBuffer;
|
|
|
|
char *buffer = smallBuffer;
|
|
|
|
int result;
|
|
|
|
|
|
|
|
va_start(args, format);
|
|
|
|
n = vsnprintf(buffer, bufferSize, format, args);
|
|
|
|
va_end(args);
|
|
|
|
while (n < 0) {
|
|
|
|
if (buffer != smallBuffer)
|
|
|
|
free(buffer);
|
|
|
|
bufferSize <<= 1;
|
|
|
|
buffer = malloc(bufferSize);
|
|
|
|
if (!buffer) {
|
|
|
|
ASSERT(false);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
va_start(args, format);
|
|
|
|
n = vsnprintf(buffer, bufferSize, format, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
translateLFtoCR(buffer, n);
|
|
|
|
result = fwrite(buffer, 1, n, file);
|
|
|
|
if (buffer != smallBuffer)
|
|
|
|
free(buffer);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#else /* XP_MAC_MPW */
|
2000-01-11 00:22:43 +03:00
|
|
|
#include <SIOUX.h>
|
|
|
|
#include <MacTypes.h>
|
|
|
|
|
|
|
|
static char *mac_argv[] = {"js", 0};
|
|
|
|
|
|
|
|
static void initConsole(StringPtr consoleName, const char* startupMessage, int &argc, char **&argv)
|
|
|
|
{
|
|
|
|
SIOUXSettings.autocloseonquit = true;
|
|
|
|
SIOUXSettings.asktosaveonclose = false;
|
|
|
|
SIOUXSetTitle(consoleName);
|
|
|
|
// Set up a buffer for stderr (otherwise it's a pig).
|
|
|
|
setvbuf(stderr, new char[BUFSIZ], _IOLBF, BUFSIZ);
|
|
|
|
|
|
|
|
std::cout << startupMessage;
|
|
|
|
|
|
|
|
argc = 1;
|
|
|
|
argv = mac_argv;
|
|
|
|
}
|
2000-04-05 08:24:19 +04:00
|
|
|
#endif /* XP_MAC_MPW */
|
|
|
|
#endif /* XP_MAC */
|
2000-01-11 00:22:43 +03:00
|
|
|
|
|
|
|
|
2000-02-03 11:25:01 +03:00
|
|
|
// Interactively read a line from the input stream in and put it into s.
|
2000-01-11 00:22:43 +03:00
|
|
|
static bool promptLine(istream &in, string &s, const char *prompt)
|
|
|
|
{
|
|
|
|
std::cout << prompt;
|
|
|
|
#ifdef XP_MAC_MPW
|
|
|
|
/* Print a CR after the prompt because MPW grabs the entire line when entering an interactive command */
|
|
|
|
std::cout << std::endl;
|
|
|
|
#endif
|
2000-02-03 11:25:01 +03:00
|
|
|
#ifndef _WIN32
|
|
|
|
return std::getline(in, s) != 0;
|
|
|
|
#else
|
|
|
|
char buffer[256];
|
|
|
|
bool result = fgets(buffer, sizeof(buffer), stdin) != 0;
|
|
|
|
s = buffer;
|
|
|
|
return result;
|
|
|
|
#endif
|
2000-01-11 00:22:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-02 11:47:03 +03:00
|
|
|
static void readEvalPrint(istream &in, World &world)
|
2000-01-11 00:22:43 +03:00
|
|
|
{
|
|
|
|
String buffer;
|
|
|
|
string line;
|
2000-04-05 01:38:25 +04:00
|
|
|
String sourceLocation = widenCString("console");
|
2000-01-11 00:22:43 +03:00
|
|
|
|
|
|
|
while (promptLine(in, line, buffer.empty() ? "js> " : "")) {
|
|
|
|
if (!buffer.empty())
|
|
|
|
buffer += uni::lf;
|
2000-01-26 01:56:48 +03:00
|
|
|
appendChars(buffer, line.data(), line.size());
|
2000-01-11 00:22:43 +03:00
|
|
|
if (!buffer.empty()) {
|
2000-02-02 11:47:03 +03:00
|
|
|
try {
|
2000-04-05 01:38:25 +04:00
|
|
|
Lexer l(world, buffer, sourceLocation);
|
2000-02-02 11:47:03 +03:00
|
|
|
while (true) {
|
2000-04-05 01:38:25 +04:00
|
|
|
const Token &t = l.get(true);
|
|
|
|
if (t.hasKind(Token::end))
|
2000-02-02 11:47:03 +03:00
|
|
|
break;
|
|
|
|
String out;
|
|
|
|
out += ' ';
|
|
|
|
t.print(out, true);
|
|
|
|
showString(std::cout, out);
|
|
|
|
}
|
|
|
|
} catch (Exception &e) {
|
|
|
|
std::cout << std::endl;
|
|
|
|
showString(std::cout, e.fullMessage());
|
|
|
|
}
|
2000-01-11 00:22:43 +03:00
|
|
|
std::cout << std::endl;
|
2000-02-03 11:25:01 +03:00
|
|
|
clear(buffer);
|
2000-01-11 00:22:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
std::cout << std::endl;
|
|
|
|
#if 0
|
|
|
|
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 = lineNum;
|
|
|
|
do {
|
|
|
|
if (!GetLine(cx, bufp, file, startline == lineNum ? "js> " : "")) {
|
|
|
|
hitEOF = JS_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufp += strlen(bufp);
|
|
|
|
lineNum++;
|
|
|
|
} while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
|
|
|
|
} while (!hitEOF);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
return;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
static int ProcessInputFile(JSContext *cx, JSObject *obj, char *filename)
|
|
|
|
{
|
|
|
|
JSBool ok, hitEOF;
|
|
|
|
JSScript *script;
|
|
|
|
jsval result;
|
|
|
|
JSString *str;
|
|
|
|
char buffer[4096];
|
|
|
|
char *bufp;
|
|
|
|
int startline;
|
|
|
|
FILE *file;
|
|
|
|
|
|
|
|
if (filename && strcmp(filename, "-")) {
|
|
|
|
file = fopen(filename, "r");
|
|
|
|
if (!file) {
|
|
|
|
fprintf(stderr, "Can't open \"%s\": %s", filename, strerror(errno));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
file = stdin;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isatty(fileno(file))) {
|
|
|
|
/*
|
|
|
|
* It's not interactive - just execute it.
|
|
|
|
*
|
|
|
|
* Support the UNIX #! shell hack; gobble the first line if it starts with '#'.
|
|
|
|
*/
|
|
|
|
int ch = fgetc(file);
|
|
|
|
if (ch == '#') {
|
|
|
|
while((ch = fgetc(file)) != EOF)
|
|
|
|
if (ch == '\n' || ch == '\r')
|
|
|
|
break;
|
|
|
|
} else
|
|
|
|
ungetc(ch, file);
|
|
|
|
script = JS_CompileFileHandle(cx, obj, filename, file);
|
|
|
|
if (script)
|
|
|
|
(void)JS_ExecuteScript(cx, obj, script, &result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* It's an interactive filehandle; drop into read-eval-print loop. */
|
|
|
|
int32 lineNum = 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 = lineNum;
|
|
|
|
do {
|
|
|
|
if (!GetLine(cx, bufp, file, startline == lineNum ? "js> " : "")) {
|
|
|
|
hitEOF = JS_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
bufp += strlen(bufp);
|
|
|
|
lineNum++;
|
|
|
|
} while (!JS_BufferIsCompilableUnit(cx, obj, buffer, strlen(buffer)));
|
|
|
|
} while (!hitEOF);
|
|
|
|
fprintf(stdout, "\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
std::cerr << "usage: js [-s] [-w] [-v version] [-f scriptfile] [scriptfile] [scriptarg...]\n";
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int
|
|
|
|
ProcessArgs(char **argv, int argc)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char *filename = NULL;
|
|
|
|
jsint length;
|
|
|
|
jsval *vector;
|
|
|
|
jsval *p;
|
|
|
|
JSObject *argsObj;
|
|
|
|
JSBool isInteractive = JS_TRUE;
|
|
|
|
|
|
|
|
for (i=0; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
switch (argv[i][1]) {
|
|
|
|
case 'f':
|
|
|
|
if (i+1 == argc) {
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
filename = argv[i+1];
|
|
|
|
/* "-f -" means read from stdin */
|
|
|
|
if (filename[0] == '-' && filename[1] == '\0')
|
|
|
|
filename = NULL;
|
|
|
|
ProcessInputFile(filename);
|
|
|
|
filename = NULL;
|
|
|
|
/* XXX: js -f foo.js should interpret foo.js and then
|
|
|
|
* drop into interactive mode, but that breaks test
|
|
|
|
* harness. Just execute foo.js for now.
|
|
|
|
*/
|
|
|
|
isInteractive = JS_FALSE;
|
|
|
|
i++;
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
return usage();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
filename = argv[i++];
|
|
|
|
isInteractive = JS_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (filename || isInteractive)
|
|
|
|
ProcessInputFile(filename);
|
|
|
|
return gExitCode;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2000-03-29 23:24:07 +04:00
|
|
|
#include "icodegenerator.h"
|
2000-04-05 01:38:25 +04:00
|
|
|
static void testICG(World &world)
|
2000-03-29 23:24:07 +04:00
|
|
|
{
|
2000-04-05 01:38:25 +04:00
|
|
|
//
|
|
|
|
// testing ICG
|
|
|
|
//
|
|
|
|
uint32 pos = 0;
|
|
|
|
ICodeGenerator icg;
|
|
|
|
|
|
|
|
// var i,j; i = j + 2;
|
|
|
|
// i is bound to var #0, j to var #1
|
|
|
|
|
|
|
|
icg.beginStatement(pos);
|
|
|
|
Register r1 = icg.loadVariable(1);
|
|
|
|
Register r2 = icg.loadImmediate(2.0);
|
|
|
|
icg.saveVariable(0, icg.op(ADD, r1, r2));
|
|
|
|
|
|
|
|
// j = a.b
|
|
|
|
icg.beginStatement(pos);
|
|
|
|
Register r4 = icg.loadName(world.identifiers[widenCString("a")]);
|
|
|
|
Register r5 = icg.getProperty(world.identifiers[widenCString("b")], r4);
|
|
|
|
icg.saveVariable(1, r5);
|
|
|
|
|
|
|
|
// while (i) i = i + j;
|
|
|
|
icg.beginWhileStatement(pos);
|
|
|
|
r1 = icg.loadVariable(0);
|
|
|
|
icg.endWhileExpression(r1);
|
|
|
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadVariable(1)));
|
|
|
|
icg.endWhileStatement();
|
|
|
|
|
|
|
|
// if (i) if (j) i = 3; else j = 4;
|
|
|
|
r1 = icg.loadVariable(0);
|
|
|
|
icg.beginIfStatement(pos, r1);
|
|
|
|
r2 = icg.loadVariable(1);
|
|
|
|
icg.beginIfStatement(pos, r2);
|
|
|
|
icg.saveVariable(0, icg.loadImmediate(3));
|
|
|
|
icg.beginElseStatement(true);
|
|
|
|
icg.saveVariable(1, icg.loadImmediate(4));
|
2000-03-29 23:24:07 +04:00
|
|
|
icg.endIfStatement();
|
2000-04-05 01:38:25 +04:00
|
|
|
icg.beginElseStatement(false);
|
|
|
|
icg.endIfStatement();
|
|
|
|
|
|
|
|
|
|
|
|
// switch (i) { case 3: case 4: j = 4; break; case 5: j = 5; break; default : j = 6; }
|
|
|
|
r1 = icg.loadVariable(0);
|
|
|
|
icg.beginSwitchStatement(pos, r1);
|
|
|
|
// case 3, note empty case statement (?necessary???)
|
|
|
|
icg.endCaseCondition(icg.loadImmediate(3));
|
|
|
|
icg.beginCaseStatement();
|
|
|
|
icg.endCaseStatement();
|
|
|
|
// case 4
|
|
|
|
icg.endCaseCondition(icg.loadImmediate(4));
|
|
|
|
icg.beginCaseStatement();
|
|
|
|
icg.beginStatement(pos);
|
|
|
|
icg.saveVariable(1, icg.loadImmediate(4));
|
|
|
|
icg.breakStatement();
|
|
|
|
icg.endCaseStatement();
|
|
|
|
// case 5
|
|
|
|
icg.endCaseCondition(icg.loadImmediate(5));
|
|
|
|
icg.beginCaseStatement();
|
|
|
|
icg.beginStatement(pos);
|
|
|
|
icg.saveVariable(1, icg.loadImmediate(5));
|
|
|
|
icg.breakStatement();
|
|
|
|
icg.endCaseStatement();
|
|
|
|
// default
|
|
|
|
icg.beginDefaultStatement();
|
|
|
|
icg.beginStatement(pos);
|
|
|
|
icg.saveVariable(1, icg.loadImmediate(6));
|
|
|
|
icg.endDefaultStatement();
|
|
|
|
icg.endSwitchStatement();
|
|
|
|
|
|
|
|
// for ( ; i; i + 1 ) j = 99;
|
|
|
|
icg.beginForStatement(pos);
|
|
|
|
r1 = icg.loadVariable(0);
|
|
|
|
icg.forCondition(r1);
|
|
|
|
icg.saveVariable(0, icg.op(ADD, icg.loadVariable(0), icg.loadImmediate(1)));
|
|
|
|
icg.forIncrement();
|
|
|
|
icg.saveVariable(0, icg.loadImmediate(99));
|
|
|
|
icg.endForStatement();
|
2000-03-29 23:24:07 +04:00
|
|
|
|
2000-04-05 01:38:25 +04:00
|
|
|
InstructionStream *iCode = icg.complete();
|
|
|
|
|
|
|
|
std::cout << icg;
|
2000-03-29 23:24:07 +04:00
|
|
|
}
|
|
|
|
|
2000-04-06 06:59:16 +04:00
|
|
|
static void testInterpreter(float64 n)
|
|
|
|
{
|
|
|
|
//
|
|
|
|
// testing ICG
|
|
|
|
//
|
|
|
|
uint32 position = 0;
|
|
|
|
ICodeGenerator icg;
|
|
|
|
|
|
|
|
// generate code for factorial, and interpret it.
|
|
|
|
// fact(n) {
|
|
|
|
// n is bound to var #0.
|
|
|
|
// var result = 1;
|
|
|
|
// result is bound to var #1.
|
|
|
|
icg.beginStatement(position);
|
|
|
|
icg.saveVariable(1, icg.loadImmediate(1.0));
|
|
|
|
|
|
|
|
// while (n > 1) {
|
|
|
|
// result = result * n;
|
|
|
|
// n = n - 1;
|
|
|
|
// }
|
|
|
|
{
|
|
|
|
icg.beginWhileStatement(position);
|
|
|
|
Register r0 = icg.loadVariable(0);
|
|
|
|
Register r1 = icg.loadImmediate(1.0);
|
2000-04-07 02:40:17 +04:00
|
|
|
Register r2 = icg.op(COMPARE_GT, r0, r1);
|
|
|
|
icg.endWhileExpression(r2);
|
2000-04-06 06:59:16 +04:00
|
|
|
r0 = icg.loadVariable(0);
|
|
|
|
r1 = icg.loadVariable(1);
|
|
|
|
r2 = icg.op(MULTIPLY, r1, r0);
|
|
|
|
icg.saveVariable(1, r2);
|
|
|
|
icg.beginStatement(position);
|
|
|
|
r0 = icg.loadVariable(0);
|
|
|
|
r1 = icg.loadImmediate(1.0);
|
|
|
|
r2 = icg.op(SUBTRACT, r0, r1);
|
|
|
|
icg.saveVariable(0, r2);
|
|
|
|
icg.endWhileStatement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// return result;
|
2000-04-07 02:40:17 +04:00
|
|
|
icg.returnStatement(icg.loadVariable(1));
|
|
|
|
InstructionStream *iCode = icg.complete();
|
2000-04-06 06:59:16 +04:00
|
|
|
// std::cout << icg;
|
|
|
|
|
|
|
|
// test the iCode interpreter.
|
|
|
|
JSValues args(32);
|
|
|
|
args[0] = JSValue(n);
|
|
|
|
JSValue result = interpret(*iCode, args);
|
|
|
|
std::cout << "fact(" << n << ") = " << result.f64 << std::endl;
|
|
|
|
}
|
|
|
|
|
2000-01-11 00:22:43 +03:00
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2000-04-05 01:38:25 +04:00
|
|
|
#if defined(XP_MAC) && !defined(XP_MAC_MPW)
|
2000-01-11 00:22:43 +03:00
|
|
|
initConsole("\pJavaScript Shell", "Welcome to the js2 shell.\n", argc, argv);
|
2000-04-05 01:38:25 +04:00
|
|
|
#endif
|
2000-02-02 11:47:03 +03:00
|
|
|
World world;
|
2000-04-05 01:38:25 +04:00
|
|
|
#if 0
|
2000-04-06 06:59:16 +04:00
|
|
|
testInterpreter(5);
|
2000-04-05 01:38:25 +04:00
|
|
|
testICG(world);
|
|
|
|
#endif
|
2000-04-05 04:52:47 +04:00
|
|
|
readEvalPrint(std::cin, world);
|
2000-04-05 01:38:25 +04:00
|
|
|
return 0;
|
2000-03-29 23:24:07 +04:00
|
|
|
|
2000-01-11 00:22:43 +03:00
|
|
|
//return ProcessArgs(argv + 1, argc - 1);
|
|
|
|
}
|