RPA Toolkit
Fixed the broken rvm_scope_t class. Implemented this pointer in RJS
authorMartin Stoilov <martin@rpasearch.com>
Tue, 10 May 2011 06:49:11 +0000 (23:49 -0700)
committerMartin Stoilov <martin@rpasearch.com>
Tue, 10 May 2011 06:49:11 +0000 (23:49 -0700)
rjs/ecma262.rpa
rjs/rjscompiler.c
rjs/rjscompiler.h
rjs/rjsuids.h
rlib/rarray.h
rvm/rvmscope.c

index 2c03b0c..9448b9e 100644 (file)
@@ -81,6 +81,7 @@
 #!emitid PostfixExpressionOp                   UID_POSTFIXEXPRESSIONOP                         47
 #!emitid PrefixExpressionOp                            UID_PREFIXEXPRESSIONOP                          48
 #!emitid NewExpressionCall                             UID_NEWEXPRESSIONCALL                           49
+#!emitid FunctionExpression                            UID_FUNCTIONEXPRESSION                          50
 
 
 
@@ -210,8 +211,7 @@ MemberExpression                            ::= <MemberExpressionIndexOp>  |
                                                                        
 NewKeyword                                             ::= 'new' - 'new' <IdentifierPart>
 NewArrayExpression                             ::= <S>? 'new' <S> ( 'Array' | 'Object') <S>? <Arguments>?
-NewExpressionCall                              ::= 'new' <S>? <MemberExpression> <S>? <Arguments>
-
+NewExpressionCall                              ::= 'new' <S>? <MemberExpression> <S>? <Arguments>?
 
 
 NewExpression                                  ::= <NewArrayExpression> |
index f0cb79f..16e78b6 100644 (file)
@@ -98,6 +98,16 @@ rjs_coctx_t *rjs_compiler_getctx(rjs_compiler_t *co, rulong type)
 }
 
 
+rjs_coctx_t *rjs_compiler_gettopctx(rjs_compiler_t *co)
+{
+       rlong len = r_array_length(co->coctx);
+
+       if (len)
+               return r_array_index(co->coctx, len - 1, rjs_coctx_t*);
+       return NULL;
+}
+
+
 rlong rjs_compiler_record2opcode(rparecord_t *prec)
 {
        const rchar *input = prec->input;
@@ -534,6 +544,7 @@ rint rjs_compiler_rh_newarrayexpression(rjs_compiler_t *co, rarray_t *records, r
 
 rint rjs_compiler_rh_memberexpressiondotop(rjs_compiler_t *co, rarray_t *records, rlong rec)
 {
+       rjs_coctx_t *ctx = rjs_compiler_gettopctx(co);
        rparecord_t *prec, *pidentnamerec;
        prec = (rparecord_t *)r_array_slot(records, rec);
        rjs_compiler_debughead(co, records, rec);
@@ -541,6 +552,9 @@ rint rjs_compiler_rh_memberexpressiondotop(rjs_compiler_t *co, rarray_t *records
 
        if (rjs_compiler_playchildrecords(co, records, rec) < 0)
                return -1;
+       if (ctx && ctx->type == RJS_COCTX_FUNCTIONCALL) {
+               ((rjs_coctx_functioncall_t *)ctx)->setthis = 1;
+       }
 
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
@@ -613,15 +627,16 @@ rint rjs_compiler_rh_functiondeclaration(rjs_compiler_t *co, rarray_t *records,
        execidx = rvm_codegen_invalid_add_numlabel_s(co->cg, "__funexec", start);
        allocsidx = rvm_codegen_invalid_add_numlabel_s(co->cg, "__allocs", start);
 
-       if (rpa_record_getruleuid(records, rpa_recordtree_firstchild(records, rec, RPA_RECORD_START)) != UID_FUNCTIONNAME)
-               goto error;
-       if (rjs_compiler_record2identifer(co, records, rpa_recordtree_firstchild(records, rec, RPA_RECORD_START)) < 0)
-               goto error;
-
-       rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R1, R0, XX, 0));
+       if (rpa_record_getruleuid(records, rpa_recordtree_firstchild(records, rec, RPA_RECORD_START)) == UID_FUNCTIONNAME) {
+               if (rjs_compiler_record2identifer(co, records, rpa_recordtree_firstchild(records, rec, RPA_RECORD_START)) < 0)
+                       goto error;
+               rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, R1, R0, XX, 0));
+       }
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_JUMP, execidx, rvm_asm(RVM_MOV, R0, DA, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_SETTYPE, R0, DA, XX, RVM_DTYPE_FUNCTION));
-       rvm_codegen_addins(co->cg, rvm_asm(RVM_STRR, R0, R1, XX, 0));
+       if (rpa_record_getruleuid(records, rpa_recordtree_firstchild(records, rec, RPA_RECORD_START)) == UID_FUNCTIONNAME) {
+               rvm_codegen_addins(co->cg, rvm_asm(RVM_STRR, R0, R1, XX, 0));
+       }
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_BRANCH, endidx, rvm_asm(RVM_B, DA, XX, XX, 0));
        rvm_codegen_redefinelabel(co->cg, execidx);
        rvm_codegen_index_addrelocins(co->cg, RVM_RELOC_DEFAULT, allocsidx, rvm_asm(RVM_ADD, SP, FP, DA, 0));
@@ -630,9 +645,10 @@ rint rjs_compiler_rh_functiondeclaration(rjs_compiler_t *co, rarray_t *records,
 
        r_array_push(co->coctx, &ctx, rjs_coctx_t*);
        rvm_scope_push(co->scope);
-       if (rjs_compiler_playchildrecords(co, records, rec) < 0)
+       if (rjs_compiler_playchildrecords(co, records, rec) < 0) {
+               rvm_scope_pop(co->scope);
                goto error;
-
+       }
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
        rjs_compiler_debughead(co, records, rec);
@@ -647,7 +663,6 @@ rint rjs_compiler_rh_functiondeclaration(rjs_compiler_t *co, rarray_t *records,
        return 0;
 
 error:
-       rvm_scope_pop(co->scope);
        r_array_removelast(co->coctx);
        return -1;
 }
@@ -722,6 +737,8 @@ rint rjs_compiler_rh_functioncall(rjs_compiler_t *co, rarray_t *records, rlong r
        rec = rpa_recordtree_get(records, rec, RPA_RECORD_END);
        prec = (rparecord_t *)r_array_slot(records, rec);
        rjs_compiler_debughead(co, records, rec);
+       if (ctx.setthis)
+               rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, TP, R1, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_SUB, FP, SP, DA, ctx.arguments));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_CALL, R0, XX, XX, 0));
        rvm_codegen_addins(co->cg, rvm_asm(RVM_MOV, SP, FP, XX, 0));
@@ -1185,6 +1202,7 @@ rjs_compiler_t *rjs_compiler_create(rvmcpu_t *cpu)
        co->handlers[UID_MEMBEREXPRESSIONDOTOP] = rjs_compiler_rh_memberexpressiondotop;
        co->handlers[UID_MEMBEREXPRESSIONINDEXOP] = rjs_compiler_rh_memberexpressionindexop;
        co->handlers[UID_FUNCTIONDECLARATION] = rjs_compiler_rh_functiondeclaration;
+       co->handlers[UID_FUNCTIONEXPRESSION] = rjs_compiler_rh_functiondeclaration;
        co->handlers[UID_FUNCTIONPARAMETER] = rjs_compiler_rh_functionparameter;
        co->handlers[UID_FUNCTIONCALL] = rjs_compiler_rh_functioncall;
        co->handlers[UID_ARGUMENT] = rjs_compiler_rh_argument;
index 1ca5db8..3f819cd 100644 (file)
@@ -43,6 +43,7 @@ typedef struct rjs_coctx_function_s {
 typedef struct rjs_coctx_functioncall_s {
        rjs_coctx_t base;
        rsize_t arguments;
+       ruchar setthis;
 } rjs_coctx_functioncall_t;
 
 
index 0b31bac..32e3444 100644 (file)
@@ -48,3 +48,4 @@
 #define UID_POSTFIXEXPRESSIONOP 47
 #define UID_PREFIXEXPRESSIONOP 48
 #define UID_NEWEXPRESSIONCALL 49
+#define UID_FUNCTIONEXPRESSION 50
index ea054d6..25b430f 100644 (file)
@@ -31,8 +31,9 @@ struct rarray_s {
 #define r_array_inclast(__array__, __type__) (*((__type__*)(r_array_lastslot(__array__))) += 1)
 #define r_array_declast(__array__, __type__) (*((__type__*)(r_array_lastslot(__array__))) -= 1)
 
-#define r_array_index(__array__, __index__, __type__) (((__type__*)(void*)(__array__)->data)[__index__])
+/* #define r_array_index(__array__, __index__, __type__) (((__type__*)(void*)(__array__)->data)[__index__]) */
 #define r_array_slot(__array__, __index__) (((ruint8*)(__array__)->data) + (__array__)->elt_size * (__index__))
+#define r_array_index(__array__, __index__, __type__) *((__type__*)r_array_slot(__array__, __index__))
 #define r_array_lastslot(__array__) (r_array_length(__array__) ? r_array_slot(__array__, r_array_length(__array__) - 1) : NULL)
 #define r_array_push(__array__, __val__, __type__) do {__type__ __v__ = (__type__)__val__; r_array_add(__array__, &__v__); } while(0)
 #define r_array_pop(__array__, __type__) (r_array_index(__array__, (__array__)->len ? --(__array__)->len : 0, __type__))
index 8f15719..37cd5c2 100644 (file)
@@ -62,7 +62,7 @@ ruint rvm_scope_count(rvm_scope_t* scope)
 
 void rvm_scope_push(rvm_scope_t* scope)
 {
-       ruint scopelen = r_array_length(scope->varstack);
+       rsize_t scopelen = r_array_length(scope->varstack);
 //     r_printf("SCOPE FRAME: %d, PUSH: %d\n", r_array_length(scope->scopestack), scopelen);
        r_array_add(scope->scopestack, &scopelen);
 }
@@ -70,7 +70,7 @@ void rvm_scope_push(rvm_scope_t* scope)
 
 void rvm_scope_pop(rvm_scope_t* scope)
 {
-       ruint scopelen = r_array_index(scope->scopestack, r_array_length(scope->scopestack) - 1, ruint);
+       rsize_t scopelen = r_array_index(scope->scopestack, r_array_length(scope->scopestack) - 1, rsize_t);
        r_array_removelast(scope->scopestack);
        r_array_setlength(scope->varstack, scopelen);
 //     r_printf("SCOPE FRAME: %d, POP: %d\n", r_array_length(scope->scopestack), scopelen);
@@ -119,7 +119,7 @@ void rvm_scope_addpointer_s(rvm_scope_t *scope, const rchar *name, rpointer ptr)
 
 rvm_varmap_t *rvm_scope_lookup(rvm_scope_t *scope, const rchar *name, ruint namesize)
 {
-       ruint scopelen = r_array_length(scope->varstack);
+       rsize_t scopelen = r_array_length(scope->varstack);
        rvm_varmap_t *varmap;
        rint i;
 
@@ -137,7 +137,7 @@ rvm_varmap_t *rvm_scope_lookup(rvm_scope_t *scope, const rchar *name, ruint name
 
 rvm_varmap_t *rvm_scope_tiplookup(rvm_scope_t *scope, const rchar *name, ruint namesize)
 {
-       ruint scopelen = r_array_length(scope->varstack);
+       rsize_t scopelen = r_array_length(scope->varstack);
        ruint tipstart = r_array_empty(scope->scopestack) ? 0 : r_array_last(scope->scopestack, ruint);
        rvm_varmap_t *varmap;
        rint i;