/[jscoverage]/trunk/instrument-js.c
ViewVC logotype

Diff of /trunk/instrument-js.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 155 by siliconforks, Mon Sep 1 20:30:28 2008 UTC revision 157 by siliconforks, Sat Sep 13 03:56:45 2008 UTC
# Line 154  Line 154 
154  };  };
155    
156  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {  static void instrument_function(JSParseNode * node, Stream * f, int indent, enum FunctionType type) {
157      assert(node->pn_arity == PN_FUNC);    assert(node->pn_arity == PN_FUNC);
158      assert(ATOM_IS_OBJECT(node->pn_funAtom));    assert(ATOM_IS_OBJECT(node->pn_funAtom));
159      JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);    JSObject * object = ATOM_TO_OBJECT(node->pn_funAtom);
160      assert(JS_ObjectIsFunction(context, object));    assert(JS_ObjectIsFunction(context, object));
161      JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);    JSFunction * function = (JSFunction *) JS_GetPrivate(context, object);
162      assert(function);    assert(function);
163      assert(object == function->object);    assert(object == function->object);
164      Stream_printf(f, "%*s", indent, "");    Stream_printf(f, "%*s", indent, "");
165      if (type == FUNCTION_NORMAL) {    if (type == FUNCTION_NORMAL) {
166        Stream_write_string(f, "function");      Stream_write_string(f, "function");
167      }    }
168    
169      /* function name */    /* function name */
170      if (function->atom) {    if (function->atom) {
171        Stream_write_char(f, ' ');      Stream_write_char(f, ' ');
172        print_string_atom(function->atom, f);      print_string_atom(function->atom, f);
173      }    }
174    
175      /* function parameters */    /* function parameters */
176      Stream_write_string(f, "(");    Stream_write_string(f, "(");
177      JSAtom ** params = xnew(JSAtom *, function->nargs);    JSAtom ** params = xnew(JSAtom *, function->nargs);
178      for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
179        /* initialize to NULL for sanity check */      /* initialize to NULL for sanity check */
180        params[i] = NULL;      params[i] = NULL;
181      }    }
182      JSScope * scope = OBJ_SCOPE(object);    JSScope * scope = OBJ_SCOPE(object);
183      for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {    for (JSScopeProperty * scope_property = SCOPE_LAST_PROP(scope); scope_property != NULL; scope_property = scope_property->parent) {
184        if (scope_property->getter != js_GetArgument) {      if (scope_property->getter != js_GetArgument) {
185          continue;        continue;
186        }      }
187        assert(scope_property->flags & SPROP_HAS_SHORTID);      assert(scope_property->flags & SPROP_HAS_SHORTID);
188        assert((uint16) scope_property->shortid < function->nargs);      assert((uint16) scope_property->shortid < function->nargs);
189        assert(JSID_IS_ATOM(scope_property->id));      assert(JSID_IS_ATOM(scope_property->id));
190        params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);      params[(uint16) scope_property->shortid] = JSID_TO_ATOM(scope_property->id);
191      }    }
192      for (int i = 0; i < function->nargs; i++) {    for (int i = 0; i < function->nargs; i++) {
193        assert(params[i] != NULL);      assert(params[i] != NULL);
194        if (i > 0) {      if (i > 0) {
195          Stream_write_string(f, ", ");        Stream_write_string(f, ", ");
       }  
       if (ATOM_IS_STRING(params[i])) {  
         print_string_atom(params[i], f);  
       }  
196      }      }
197      Stream_write_string(f, ") {\n");      if (ATOM_IS_STRING(params[i])) {
198      free(params);        print_string_atom(params[i], f);
199        }
200      }
201      Stream_write_string(f, ") {\n");
202      free(params);
203    
204      /* function body */    /* function body */
205      instrument_statement(node->pn_body, f, indent + 2);    instrument_statement(node->pn_body, f, indent + 2);
206    
207      Stream_write_string(f, "}\n");    Stream_write_string(f, "}\n");
208  }  }
209    
210  static void instrument_function_call(JSParseNode * node, Stream * f) {  static void instrument_function_call(JSParseNode * node, Stream * f) {
# Line 877  Line 877 
877    free(lines);    free(lines);
878    lines = NULL;    lines = NULL;
879    
880      /* conditionals */
881      bool has_conditionals = false;
882      size_t line_number = 0;
883      size_t i = 0;
884      while (i < input_length) {
885        line_number++;
886        size_t line_start = i;
887        while (i < input_length && base[i] != '\r' && base[i] != '\n') {
888          i++;
889        }
890        size_t line_end = i;
891        if (i < input_length) {
892          if (base[i] == '\r') {
893            line_end = i;
894            i++;
895            if (i < input_length && base[i] == '\n') {
896              i++;
897            }
898          }
899          else if (base[i] == '\n') {
900            line_end = i;
901            i++;
902          }
903          else {
904            abort();
905          }
906        }
907        char * line = js_DeflateString(context, base + line_start, line_end - line_start);
908        if (str_starts_with(line, "//#JSCOVERAGE_IF")) {
909          if (! has_conditionals) {
910            has_conditionals = true;
911            Stream_printf(output, "_$jscoverage['%s'].conditionals = [];\n", file_id);
912          }
913          Stream_printf(output, "_$jscoverage['%s'].conditionals[%d] = {condition: function () {return %s;}, ", file_id, line_number, line + 16);
914        }
915        else if (str_starts_with(line, "//#JSCOVERAGE_ENDIF")) {
916          Stream_printf(output, "end: %d};\n", line_number);
917        }
918        JS_free(context, line);
919      }
920    
921    /* copy the instrumented source code to the output */    /* copy the instrumented source code to the output */
922    Stream_write(output, instrumented->data, instrumented->length);    Stream_write(output, instrumented->data, instrumented->length);
923    Stream_write_char(output, '\n');    Stream_write_char(output, '\n');
924    
925    /* copy the original source to the output */    /* copy the original source to the output */
926    size_t i = 0;    i = 0;
927    while (i < input_length) {    while (i < input_length) {
928      Stream_write_string(output, "// ");      Stream_write_string(output, "// ");
929      size_t line_start = i;      size_t line_start = i;

Legend:
Removed from v.155  
changed lines
  Added in v.157

  ViewVC Help
Powered by ViewVC 1.1.24