Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Last active October 11, 2017 07:26
Show Gist options
  • Save gabrielschulhof/a7a4de122349e67fc79c9ed533ad4730 to your computer and use it in GitHub Desktop.
Save gabrielschulhof/a7a4de122349e67fc79c9ed533ad4730 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include "test-common.h"
#include "jerryscript.h"
static jerry_value_t
native_function (const jerry_value_t function_obj,
const jerry_value_t this_val,
const jerry_value_t args_p[],
const jerry_value_t args_count)
{
(void) function_obj;
(void) this_val;
(void) args_p;
(void) args_count;
return jerry_create_undefined();
} /* native_function */
static void
eval_js(char *js_code, char *result)
{
jerry_value_t parse_result = jerry_parse ((jerry_char_t *) js_code,
strlen (js_code),
true);
TEST_ASSERT (!jerry_value_has_error_flag (parse_result));
jerry_value_t eval_result = jerry_run (parse_result);
TEST_ASSERT (!jerry_value_has_error_flag (eval_result));
TEST_ASSERT (jerry_value_is_string (eval_result));
jerry_size_t result_size = jerry_get_string_size (eval_result);
jerry_char_t result_string[result_size + 1];
jerry_string_to_char_buffer (eval_result, result_string, result_size);
result_string[result_size] = 0;
printf("%s -> %s\n", js_code, result_string);
if (result != NULL)
{
TEST_ASSERT (!strcmp ((char *) result_string, result));
}
jerry_release_value (parse_result);
jerry_release_value (eval_result);
} /* eval_js */
int
main (int argc, char **argv)
{
(void) argc;
(void) argv;
jerry_init (JERRY_INIT_EMPTY);
jerry_value_t js_native_function = jerry_create_external_function (native_function);
jerry_value_t global = jerry_get_global_object ();
jerry_value_t name = jerry_create_string ((jerry_char_t *) "native_function");
jerry_value_t set_result = jerry_set_property (global, name, js_native_function);
TEST_ASSERT (!jerry_value_has_error_flag (set_result));
jerry_value_t prototype = jerry_create_object ();
jerry_release_value (set_result);
set_result = jerry_set_prototype (js_native_function, prototype);
TEST_ASSERT(!jerry_value_has_error_flag (set_result));
jerry_value_t proto_prop = jerry_create_string ((jerry_char_t *) "some_property");
jerry_value_t proto_prop_value = jerry_create_string ((jerry_char_t *) "some_value");
jerry_release_value (set_result);
set_result = jerry_set_property (prototype, proto_prop, proto_prop_value);
TEST_ASSERT (!jerry_value_has_error_flag (set_result));
eval_js ("typeof native_function.prototype", NULL);
eval_js ("typeof native_function.__proto__", NULL);
eval_js ("typeof (new native_function()).some_property", NULL);
jerry_release_value (proto_prop_value);
jerry_release_value (proto_prop);
jerry_release_value (prototype);
jerry_release_value (set_result);
jerry_release_value (name);
jerry_release_value (global);
jerry_release_value (js_native_function);
jerry_cleanup();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment