RPA Toolkit
Added break, continue implementation to RJS.
[rpatk.git] / rjs / rjs.c
1 #include "rmem.h"
2 #include "rjs.h"
3
4
5 static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins);
6 static void rjs_engine_dbgprint(rvmcpu_t *cpu, rvm_asmins_t *ins);
7 static void rjs_engine_object(rvmcpu_t *cpu, rvm_asmins_t *ins);
8 static rvm_switable_t rjsswitable[] = {
9                 {"print", rjs_engine_print},
10                 {"dbgprint", rjs_engine_dbgprint},
11                 {"Object", rjs_engine_object},
12                 {"Array", rjs_engine_object},
13                 {NULL, NULL},
14 };
15
16
17 const rchar *rjs_version()
18 {
19         return RJS_VERSION_STRING;
20 }
21
22
23 rjs_engine_t *rjs_engine_create()
24 {
25         rjs_engine_t *jse = (rjs_engine_t *) r_zmalloc(sizeof(*jse));
26
27         jse->pa = rjs_parser_create();
28         jse->cpu = rvm_cpu_create_default();
29         rvm_cpu_addswitable(jse->cpu, "rjsswitable", rjsswitable);
30         return jse;
31 }
32
33
34 void rjs_engine_destroy(rjs_engine_t *jse)
35 {
36         if (jse) {
37                 rjs_parser_destroy(jse->pa);
38                 rvm_cpu_destroy(jse->cpu);
39                 rjs_compiler_destroy(jse->co);
40                 r_free(jse);
41         }
42 }
43
44
45 rint rjs_engine_open(rjs_engine_t *jse)
46 {
47         return 0;
48 }
49
50
51 static rint rjs_engine_parse(rjs_engine_t *jse, const rchar *script, rsize_t size, rarray_t **records)
52 {
53         rlong res = 0;
54         res = rjs_parser_exec(jse->pa, script, size, records);
55         return res;
56 }
57
58
59 rint rjs_engine_compile(rjs_engine_t *jse, const rchar *script, rsize_t size)
60 {
61         rarray_t *records = NULL;
62         if (jse->co)
63                 rjs_compiler_destroy(jse->co);
64         jse->co = rjs_compiler_create(jse->cpu);
65         jse->co->debug = jse->debugcompile;
66
67         if (rjs_engine_parse(jse, script, size, &records) < 0) {
68
69                 goto err;
70         }
71
72         if (rjs_compiler_compile(jse->co, records) < 0) {
73
74                 goto err;
75         }
76
77         r_array_destroy(records);
78         return 0;
79
80 err:
81         r_array_destroy(records);
82         return -1;
83 }
84
85
86 rint rjs_engine_dumpast(rjs_engine_t *jse, const rchar *script, rsize_t size)
87 {
88         rarray_t *records = NULL;
89         if (rjs_engine_parse(jse, script, size, &records) < 0) {
90
91
92                 return -1;
93         }
94
95         if (records) {
96                 rlong i;
97                 for (i = 0; i < r_array_length(records); i++)
98                         rpa_record_dump(records, i);
99         }
100
101         r_array_destroy(records);
102         return 0;
103 }
104
105
106 rint rjs_engine_compile_s(rjs_engine_t *jse, const rchar *script)
107 {
108         return rjs_engine_compile(jse, script, r_strlen(script));
109 }
110
111
112 rint rjs_engine_close(rjs_engine_t *jse)
113 {
114
115         return 0;
116 }
117
118
119 rint rjs_engine_run(rjs_engine_t *jse)
120 {
121         rint res = 0;
122
123         if (!jse->co) {
124
125                 return -1;
126         }
127         if (jse->debugexec) {
128                 res = rvm_cpu_exec_debug(jse->cpu, rvm_codegen_getcode(jse->co->cg, 0), 0);
129         } else {
130                 res = rvm_cpu_exec(jse->cpu, rvm_codegen_getcode(jse->co->cg, 0), 0);
131         }
132         return res;
133 }
134
135
136 static void rjs_engine_print(rvmcpu_t *cpu, rvm_asmins_t *ins)
137 {
138         rvmreg_t *r = (rvmreg_t *)RVM_STACK_ADDR(cpu->stack, RVM_CPUREG_GETU(cpu, FP) + 1);
139
140         if (rvm_reg_gettype(r) == RVM_DTYPE_UNSIGNED)
141                 r_printf("%lu\n", RVM_REG_GETU(r));
142         else if (rvm_reg_gettype(r) == RVM_DTYPE_NAN)
143                 r_printf("NaN\n");
144         else if (rvm_reg_gettype(r) == RVM_DTYPE_UNDEF)
145                 r_printf("undefined\n");
146         else if (rvm_reg_gettype(r) == RVM_DTYPE_BOOLEAN)
147                 r_printf("%s\n", RVM_REG_GETU(r) ? "true" : "false");
148         else if (rvm_reg_gettype(r) == RVM_DTYPE_POINTER)
149                 r_printf("%p\n", RVM_REG_GETP(r));
150         else if (rvm_reg_gettype(r) == RVM_DTYPE_LONG)
151                 r_printf("%ld\n", RVM_REG_GETL(r));
152         else if (rvm_reg_gettype(r) == RVM_DTYPE_DOUBLE)
153                 r_printf("%f\n", RVM_REG_GETD(r));
154         else if (rvm_reg_gettype(r) == RVM_DTYPE_STRING)
155                 r_printf("%s\n", ((rstring_t*) RVM_REG_GETP(r))->s.str);
156         else if (rvm_reg_gettype(r) == RVM_DTYPE_JSOBJECT)
157                 r_printf("(object) %p\n",RVM_REG_GETP(r));
158         else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
159                 r_printf("(hashed array) %p\n",RVM_REG_GETP(r));
160         else if (rvm_reg_gettype(r) == RVM_DTYPE_SWIID)
161                 r_printf("(function) %p\n",RVM_REG_GETP(r));
162         else
163                 r_printf("%p\n", RVM_REG_GETP(r));
164 }
165
166
167 static void rjs_engine_dbgprint(rvmcpu_t *cpu, rvm_asmins_t *ins)
168 {
169         rvmreg_t *r = (rvmreg_t *)RVM_STACK_ADDR(cpu->stack, RVM_CPUREG_GETU(cpu, FP) + 1);
170
171         if (rvm_reg_gettype(r) == RVM_DTYPE_UNSIGNED)
172                 r_printf("(UNSIGNED) %lu\n", RVM_REG_GETU(r));
173         else if (rvm_reg_gettype(r) == RVM_DTYPE_NAN)
174                 r_printf("(NAN) NaN\n");
175         else if (rvm_reg_gettype(r) == RVM_DTYPE_UNDEF)
176                 r_printf("(UNDEF) undefined\n");
177         else if (rvm_reg_gettype(r) == RVM_DTYPE_BOOLEAN)
178                 r_printf("(BOOLEAN) %s\n", RVM_REG_GETU(r) ? "true" : "false");
179         else if (rvm_reg_gettype(r) == RVM_DTYPE_POINTER)
180                 r_printf("(POINTER) %p\n", RVM_REG_GETP(r));
181         else if (rvm_reg_gettype(r) == RVM_DTYPE_LONG)
182                 r_printf("(LONG) %ld\n", RVM_REG_GETL(r));
183         else if (rvm_reg_gettype(r) == RVM_DTYPE_DOUBLE)
184                 r_printf("(DOUBLE) %f\n", RVM_REG_GETD(r));
185         else if (rvm_reg_gettype(r) == RVM_DTYPE_STRING)
186                 r_printf("(STRING) %s\n", ((rstring_t*) RVM_REG_GETP(r))->s.str);
187         else if (rvm_reg_gettype(r) == RVM_DTYPE_JSOBJECT)
188                 r_printf("(object) %p\n",RVM_REG_GETP(r));
189         else if (rvm_reg_gettype(r) == RVM_DTYPE_HARRAY)
190                 r_printf("(hashed array) %p\n",RVM_REG_GETP(r));
191         else if (rvm_reg_gettype(r) == RVM_DTYPE_SWIID)
192                 r_printf("(function) %p\n",RVM_REG_GETP(r));
193         else
194                 r_printf("%p\n", RVM_REG_GETP(r));
195 }
196
197
198 static void rjs_engine_object(rvmcpu_t *cpu, rvm_asmins_t *ins)
199 {
200
201 }