Ejemplo n.º 1
0
/**
 * Unit test's main function.
 */
int
main (int __attr_unused___ argc,
      char __attr_unused___ **argv)
{
  TEST_INIT ();

  const bytecode_data_header_t *bytecode_data_p;
  jsp_status_t parse_status;

  mem_init ();

  // #1
  char program1[] = "a=1;var a;";

  serializer_init ();
  parser_set_show_instrs (true);
  parse_status = parser_parse_script ((jerry_api_char_t *) program1, strlen (program1), &bytecode_data_p);

  JERRY_ASSERT (parse_status == JSP_STATUS_OK && bytecode_data_p != NULL);

  vm_instr_t instrs[] =
  {
    getop_meta (OPCODE_META_TYPE_SCOPE_CODE_FLAGS, // [ ]
                OPCODE_SCOPE_CODE_FLAGS_NOT_REF_ARGUMENTS_IDENTIFIER
                | OPCODE_SCOPE_CODE_FLAGS_NOT_REF_EVAL_IDENTIFIER,
                VM_IDX_EMPTY),
    getop_reg_var_decl (VM_REG_FIRST, VM_REG_GENERAL_FIRST, 0),
    getop_var_decl (0),             // var a;
    getop_assignment (130, 1, 1),   // $tmp0 = 1;
    getop_assignment (0, 6, 130),   // a = $tmp0;
    getop_ret ()                    // return;
  };

  JERRY_ASSERT (instrs_equal (bytecode_data_p->instrs_p, instrs, 5));

  serializer_free ();

  // #2
  char program2[] = "var var;";

  serializer_init ();
  parser_set_show_instrs (true);
  parse_status = parser_parse_script ((jerry_api_char_t *) program2, strlen (program2), &bytecode_data_p);

  JERRY_ASSERT (parse_status == JSP_STATUS_SYNTAX_ERROR && bytecode_data_p == NULL);

  serializer_free ();

  mem_finalize (false);

  return 0;
} /* main */
Ejemplo n.º 2
0
/**
 * Parse EcamScript source code
 */
jsp_status_t
parser_parse_script (const jerry_api_char_t *source_p, /**< source code */
                     size_t size, /**< size of the source code */
                     ecma_compiled_code_t **bytecode_data_p) /**< result */
{
  *bytecode_data_p = parser_parse_script (source_p, size, false, NULL);

  if (!*bytecode_data_p)
  {
    return JSP_STATUS_SYNTAX_ERROR;
  }

  return JSP_STATUS_OK;
} /* parser_parse_script */