30 #include "interpreter.h"
34 class TestFunctionImp :
public ObjectImp {
36 TestFunctionImp(
int i,
int length);
37 virtual bool implementsCall()
const {
return true; }
40 enum { Print, Debug, Quit };
46 TestFunctionImp::TestFunctionImp(
int i,
int length) : ObjectImp(), id(i)
48 putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
56 fprintf(stderr,
"--> %s\n",args[0].toString(exec).ascii());
68 class VersionFunctionImp :
public ObjectImp {
70 VersionFunctionImp() : ObjectImp() {}
71 virtual bool implementsCall()
const {
return true; }
82 class GlobalImp :
public ObjectImp {
84 virtual UString className()
const {
return "global"; }
87 int main(
int argc,
char **argv)
91 fprintf(stderr,
"You have to specify at least one filename\n");
97 Object global(
new GlobalImp());
102 global.put(interp.globalExec(),
"debug",
Object(
new TestFunctionImp(TestFunctionImp::Debug,1)));
104 global.put(interp.globalExec(),
"print",
Object(
new TestFunctionImp(TestFunctionImp::Print,1)));
106 global.put(interp.globalExec(),
"quit",
Object(
new TestFunctionImp(TestFunctionImp::Quit,0)));
108 global.put(interp.globalExec(),
"version",
Object(
new VersionFunctionImp()));
110 for (
int i = 1; i < argc; i++) {
112 int code_alloc = 1024;
113 char *code = (
char*)malloc(code_alloc);
115 const char *file = argv[i];
116 if (strcmp(file,
"-f") == 0)
118 FILE *f = fopen(file,
"r");
120 fprintf(stderr,
"Error opening %s.\n", file);
124 while (!feof(f) && !ferror(f)) {
125 size_t len = fread(code+code_len,1,code_alloc-code_len,f);
127 if (code_len >= code_alloc) {
129 code = (
char*)realloc(code,code_alloc);
132 code = (
char*)realloc(code,code_len+1);
133 code[code_len] =
'\0';
140 if (comp.complType() == Throw) {
142 Value exVal = comp.value();
145 if (exVal.
type() == ObjectType) {
146 Value lineVal = Object::dynamicCast(exVal).get(exec,
"line");
147 if (lineVal.
type() == NumberType)
148 lineno = int(lineVal.
toNumber(exec));
151 fprintf(stderr,
"Exception, line %d: %s\n",lineno,msg);
153 fprintf(stderr,
"Exception: %s\n",msg);
156 else if (comp.complType() == ReturnValue) {
157 char *msg = comp.value().toString(interp.globalExec()).ascii();
158 fprintf(stderr,
"Return value: %s\n",msg);
167 fprintf(stderr,
"OK.\n");
170 Interpreter::finalCheck();
Completion objects are used to convey the return status and value from functions.
Represents the current state of script execution.
Interpreter objects can be used to evaluate ECMAScript code.
char * ascii() const
Convert the Unicode string to plain ASCII chars chopping of any higher bytes.
Represents an primitive Undefined value.
Value objects are act as wrappers ("smart pointers") around ValueImp objects and their descendents.
UString toString(ExecState *exec) const
Performs the ToString type conversion operation on this value (ECMA 9.8)
Type type() const
Returns the type of value.
double toNumber(ExecState *exec) const
Performs the ToNumber type conversion operation on this value (ECMA 9.3)