29 #include "interpreter.h"
33 class TestFunctionImp :
public ObjectImp {
35 TestFunctionImp(
int i,
int length);
36 virtual bool implementsCall()
const {
return true; }
39 enum { Print, Debug, Quit };
45 TestFunctionImp::TestFunctionImp(
int i,
int length) : ObjectImp(), id(i)
47 putDirect(lengthPropertyName,length,DontDelete|ReadOnly|DontEnum);
55 fprintf(stderr,
"--> %s\n",args[0].toString(exec).ascii());
67 class VersionFunctionImp :
public ObjectImp {
69 VersionFunctionImp() : ObjectImp() {}
70 virtual bool implementsCall()
const {
return true; }
81 class GlobalImp :
public ObjectImp {
83 virtual UString className()
const {
return "global"; }
86 int main(
int argc,
char **argv)
90 fprintf(stderr,
"You have to specify at least one filename\n");
96 Object global(
new GlobalImp());
101 global.put(interp.globalExec(),
"debug",
Object(
new TestFunctionImp(TestFunctionImp::Debug,1)));
103 global.put(interp.globalExec(),
"print",
Object(
new TestFunctionImp(TestFunctionImp::Print,1)));
105 global.put(interp.globalExec(),
"quit",
Object(
new TestFunctionImp(TestFunctionImp::Quit,0)));
107 global.put(interp.globalExec(),
"version",
Object(
new VersionFunctionImp()));
109 for (
int i = 1; i < argc; i++) {
111 int code_alloc = 1024;
112 char *code = (
char*)malloc(code_alloc);
114 const char *file = argv[i];
115 if (strcmp(file,
"-f") == 0)
117 FILE *f = fopen(file,
"r");
119 fprintf(stderr,
"Error opening %s.\n", file);
123 while (!feof(f) && !ferror(f)) {
124 size_t len = fread(code+code_len,1,code_alloc-code_len,f);
126 if (code_len >= code_alloc) {
128 code = (
char*)realloc(code,code_alloc);
131 code = (
char*)realloc(code,code_len+1);
132 code[code_len] =
'\0';
139 if (comp.complType() == Throw) {
141 Value exVal = comp.value();
144 if (exVal.
type() == ObjectType) {
145 Value lineVal = Object::dynamicCast(exVal).get(exec,
"line");
146 if (lineVal.
type() == NumberType)
147 lineno = int(lineVal.
toNumber(exec));
150 fprintf(stderr,
"Exception, line %d: %s\n",lineno,msg);
152 fprintf(stderr,
"Exception: %s\n",msg);
155 else if (comp.complType() == ReturnValue) {
156 char *msg = comp.value().toString(interp.globalExec()).ascii();
157 fprintf(stderr,
"Return value: %s\n",msg);
166 fprintf(stderr,
"OK.\n");
169 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)