Example #1
0
/*
 * call-seq:
 *   ctx_define_function(name, &block) -> nil
 *
 * Define a function defined in the global scope and identified by a name.
 *
 *     ctx.ctx_define_function("hello_world") { |ctx| 'Hello world' } #=> nil
 *
 */
static VALUE ctx_define_function(VALUE self, VALUE prop)
{
  VALUE block;
  struct state *state;
  duk_context *ctx;

  // a block is required
  if (!rb_block_given_p())
    rb_raise(rb_eArgError, "Expected block");

  // get the context
  Data_Get_Struct(self, struct state, state);
  ctx = state->ctx;

  // the c function is available in the global scope
  duk_push_global_object(ctx);

  duk_push_c_function(ctx, ctx_call_pushed_function, DUK_VARARGS);

  block = rb_block_proc();
  rb_ary_push(state->blocks, block); // block will be properly garbage collected

  // both block and state are required by the pushed function
  duk_push_string(ctx, "block");
  duk_push_pointer(ctx, (void *) block);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_push_string(ctx, "state");
  duk_push_pointer(ctx, (void *) state);
  duk_def_prop(ctx, -3,  DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_HAVE_WRITABLE | 0);

  duk_put_prop_string(ctx, -2, StringValueCStr(prop));

  return Qnil;
}
Example #2
0
// Load a duktape C function from a shared library by path and name.
static duk_ret_t duv_loadlib(duk_context *ctx) {
  const char *name, *path;
  uv_lib_t lib;
  duk_c_function fn;

  // Check the args
  const duv_schema_entry schema[] = {
    { "path", duk_is_string },
    { "name", duk_is_string },
    { NULL, NULL }
  };

  dschema_check(ctx, schema);

  path = duk_get_string(ctx, 0);
  name = duk_get_string(ctx, 1);

  if (uv_dlopen(path, &lib)) {
    duk_error(ctx, DUK_ERR_ERROR, "Cannot load shared library %s", path);
    return 0;
  }
  if (uv_dlsym(&lib, name, (void**)&fn)) {
    duk_error(ctx, DUK_ERR_ERROR, "Unable to find %s in %s", name, path);
    return 0;
  }
  duk_push_c_function(ctx, fn, 0);
  return 1;
}
void
_gum_duk_process_init (GumDukProcess * self,
                       GumDukCore * core)
{
  GumDukScope scope = GUM_DUK_SCOPE_INIT (core);
  duk_context * ctx = scope.ctx;

  self->core = core;

  duk_push_c_function (ctx, gumjs_process_construct, 0);
  duk_push_object (ctx);
  duk_put_function_list (ctx, -1, gumjs_process_functions);
  duk_push_string (ctx, GUM_SCRIPT_ARCH);
  duk_put_prop_string (ctx, -2, "arch");
  duk_push_string (ctx, GUM_SCRIPT_PLATFORM);
  duk_put_prop_string (ctx, -2, "platform");
  duk_push_uint (ctx, gum_query_page_size ());
  duk_put_prop_string (ctx, -2, "pageSize");
  duk_push_uint (ctx, GLIB_SIZEOF_VOID_P);
  duk_put_prop_string (ctx, -2, "pointerSize");
  duk_put_prop_string (ctx, -2, "prototype");
  duk_new (ctx, 0);
  _gum_duk_put_data (ctx, -1, self);
  duk_put_global_string (ctx, "Process");
}
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

	/* Lightfunc, must sanitize the address for the expect string. */
	duk_eval_string(ctx,
		"(function (v) {\n"
		"    print(String(v).replace(/light_[0-9a-fA-f_]+/, 'light_PTR'));\n"
		"})");
	duk_push_c_lightfunc(ctx, dummy_func, 0 /*nargs*/, 0 /*length*/, 0 /*magic*/);
	duk_call(ctx, 1);
	duk_pop(ctx);

	/* Function with a .name containing invalid characters.
	 *
	 * This is not currently handled very well: the .toString() output
	 * uses the name as is, which can make the output technically
	 * incorrect because it won't parse as a function.  However, the
	 * only way to create such functions is from C code so this is a
	 * minor issue.
	 */

	duk_push_c_function(ctx, dummy_func, 0 /*nargs*/);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, "dummy {");
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);
	printf("%s\n", duk_to_string(ctx, -1));
	duk_pop(ctx);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}
Example #5
0
int main(int argc, const char *argv[]) {
    duk_context *ctx = NULL;

    ctx = duk_create_heap_default();
    if (!ctx) {
        printf("Failed to create a Duktape heap.\n");
        exit(1);
    }

    duk_push_global_object(ctx);
    duk_push_c_function(ctx, native_prime_check, 2 /*nargs*/);
    duk_put_prop_string(ctx, -2, "primeCheckNative");

    if (duk_peval_file(ctx, "prime.js") != 0) {
        printf("Error: %s\n", duk_safe_to_string(ctx, -1));
        goto finished;
    }
    duk_pop(ctx);  /* ignore result */

    duk_get_prop_string(ctx, -1, "primeTest");
    if (duk_pcall(ctx, 0) != 0) {
        printf("Error: %s\n", duk_safe_to_string(ctx, -1));
    }
    duk_pop(ctx);  /* ignore result */

 finished:
    duk_destroy_heap(ctx);

    exit(0);
}
Example #6
0
void test(duk_context *ctx) {
	int i, n;

	duk_push_c_function(ctx, func, 0);

	duk_push_undefined(ctx);
	duk_push_null(ctx);
	duk_push_true(ctx);
	duk_push_false(ctx);
	duk_push_number(ctx, 123.456);
	duk_push_string(ctx, "foo");
	duk_push_object(ctx);
	duk_push_array(ctx);
	duk_push_fixed_buffer(ctx, 16);
	duk_push_pointer(ctx, (void *) 0xdeadbeef);

	n = duk_get_top(ctx);
	printf("top: %d\n", n);
	for (i = 1; i < n; i++) {
		duk_dup(ctx, 0);
		duk_dup(ctx, i);
		duk_call_method(ctx, 0);  /* [ ... func this ] -> [ ret ] */
		duk_pop(ctx);
	}
}
Example #7
0
    static Box * PrivatePush( duk_context * ctx, const size_t class_index, const size_t object_size, finalizer_t finalizer )
    {
        duk_push_global_object( ctx );
        duk_get_prop_string( ctx, -1, "Proxy" );
        duk_remove( ctx, -2 );

        duk_push_object( ctx );

        size_t require_size = sizeof( Box ) + object_size;

        Box * box = reinterpret_cast<Box*>( duk_push_fixed_buffer( ctx, require_size ) );
        box->ClassIndex = class_index;
        box->Finalizer = finalizer;

        duk_put_prop_string( ctx, -2, "\xFF" "Box" );

        duk_push_c_function( ctx, &internal::ClassFinalizer, 1 );
        duk_set_finalizer( ctx, -2 );

        duk_push_heap_stash( ctx );
        duk_get_prop_string( ctx, -1, "InstanceHandler" );
        duk_remove( ctx, -2 );
        duk_new( ctx, 2 );

        return box;
    }
Example #8
0
int duk_import_java_class(duk_context *ctx){
	DEBUG_LOG("DuktapeEngine", "className duk_import_java_class");
	int n = duk_get_top(ctx);
	const char* className = duk_to_string(ctx, 0);
	const char*  shortName = NULL;
	if(n > 1){
		shortName = duk_to_string(ctx, 1);
	}else{
		shortName = strrchr(className, '.');
		shortName++;
	}
	DEBUG_LOG("DuktapeEngine", "className %s  shorName %s",className, shortName);

	duk_push_global_object(ctx);
	duk_push_c_function(ctx, duk_new_java_class,  DUK_VARARGS);

	JNIEnv *env = get_java_jni_env();
	jstring  fullClassName = (*env)->NewStringUTF(env, className);
    jobject  value =  (*env)->CallStaticObjectMethod(env, java_api_class, java_import_class_method, fullClassName);
	duk_mark_jsobject_to_java_object(ctx, -1, env, value);
    (*env)->DeleteLocalRef(env, value);
	(*env)->DeleteLocalRef(env, fullClassName);
	duk_push_object(ctx);
    duk_push_string(ctx, className);
    duk_put_prop_string(ctx, -2, "className");
    duk_put_prop_string(ctx, -2, "prototype");
    duk_put_prop_string(ctx, -2, shortName);
    duk_pop(ctx);
    return 0;
}
Example #9
0
// Constructor of the JS Object
duk_ret_t js_watchdog_ctor(duk_context *ctx)
{
    void *ptr = NULL;
    slog(DEBUG,DEBUG, "Creating new object of "PLUGIN_SCOPE);

    // Push special this binding to the function being constructed
    duk_push_this(ctx);

    // Store the underlying object
    duk_push_pointer(ctx, ptr);
    duk_put_prop_string(ctx, -2, "\xff""\xff""data");

    // TODO : - if not existand create a hash_map
    //        - store structure to a hash_map('name');
    //          so that it can be reached from JS and C
    
    // Store a boolean flag to mark the object as deleted because the destructor may be called several times
    duk_push_boolean(ctx, false);
    duk_put_prop_string(ctx, -2, "\xff""\xff""deleted");

    // Store the function destructor
    duk_push_c_function(ctx, js_watchdog_dtor, 1);
    duk_set_finalizer(ctx, -2);

    return 0;
}
Example #10
0
/* Thread with shared and fresh globals. */
static duk_ret_t test_2(duk_context *ctx, void *udata) {
	duk_context *ctx_a;
	duk_context *ctx_b;

	(void) udata;

	duk_eval_string_noresult(ctx, "this.globalFoo = 'bar';");

	duk_push_thread(ctx);
	ctx_a = duk_require_context(ctx, -1);
	duk_push_thread_new_globalenv(ctx);
	ctx_b = duk_require_context(ctx, -1);

	printf("top: %ld\n", (long) duk_get_top(ctx));

	/* Dummy print() binding. */
	duk_push_c_function(ctx_b, my_print, 1);
	duk_put_global_string(ctx_b, "print");

	/* index 0: thread with globals shared with 'ctx' (has globalFoo)
	 * index 1: thread with globals separate with 'ctx'
	 */

	/* Print globalFoo. */

	duk_peval_string_noresult(ctx_a, "print('context a: ' + String(this.globalFoo));");
	duk_peval_string_noresult(ctx_b, "print('context b: ' + String(this.globalFoo));");

	return 0;
}
static duk_ret_t PhysicsWorld_Get_Updated(duk_context* ctx)
{
    PhysicsWorld* thisObj = GetThisWeakObject<PhysicsWorld>(ctx);
    SignalWrapper_PhysicsWorld_Updated* wrapper = new SignalWrapper_PhysicsWorld_Updated(thisObj, &thisObj->Updated);
    PushValueObject(ctx, wrapper, SignalWrapper_PhysicsWorld_Updated_ID, SignalWrapper_PhysicsWorld_Updated_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_PhysicsWorld_Updated_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_PhysicsWorld_Updated_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_PhysicsWorld_Updated_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_PhysicsWorld_Updated_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_PhysicsWorld_Updated_Emit, 1);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
Example #12
0
void Context::registerFunc(index_t idx,
                           const char* id,
                           const Function func,
                           index_t nargs)
{
    duk_push_c_function(m_handle, func, nargs);
    duk_put_prop_string(m_handle, idx, id);
}
static duk_ret_t Avatar_Get_ParentEntitySet(duk_context* ctx)
{
    Avatar* thisObj = GetThisWeakObject<Avatar>(ctx);
    SignalWrapper_Avatar_ParentEntitySet* wrapper = new SignalWrapper_Avatar_ParentEntitySet(thisObj, &thisObj->ParentEntitySet);
    PushValueObject(ctx, wrapper, SignalWrapper_Avatar_ParentEntitySet_ID, SignalWrapper_Avatar_ParentEntitySet_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_Avatar_ParentEntitySet_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ParentEntitySet_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ParentEntitySet_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ParentEntitySet_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ParentEntitySet_Emit, 0);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
static duk_ret_t Avatar_Get_ComponentNameChanged(duk_context* ctx)
{
    Avatar* thisObj = GetThisWeakObject<Avatar>(ctx);
    SignalWrapper_Avatar_ComponentNameChanged* wrapper = new SignalWrapper_Avatar_ComponentNameChanged(thisObj, &thisObj->ComponentNameChanged);
    PushValueObject(ctx, wrapper, SignalWrapper_Avatar_ComponentNameChanged_ID, SignalWrapper_Avatar_ComponentNameChanged_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_Avatar_ComponentNameChanged_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ComponentNameChanged_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ComponentNameChanged_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ComponentNameChanged_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_Avatar_ComponentNameChanged_Emit, 2);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
Example #15
0
void Engine::registerFunction(const char* id, Function function, int nargs)
{
  ContextHandle handle = m_ctx.handle();

  duk_push_global_object(handle);
  duk_push_c_function(handle, function, nargs);
  duk_put_prop_string(handle, -2, id);
}
Example #16
0
duk_ret_t dukky_html_menu_element___proto(duk_context *ctx)
{
	/* Set this prototype's prototype (left-parent) */
	/* get prototype */
	duk_get_global_string(ctx, dukky_magic_string_prototypes);
	duk_get_prop_string(ctx, -1, "\xFF\xFFNETSURF_DUKTAPE_PROTOTYPE_HTMLELEMENT");
	duk_replace(ctx, -2);
	duk_set_prototype(ctx, 0);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "type");
	duk_push_c_function(ctx, dukky_html_menu_element_type_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_type_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "label");
	duk_push_c_function(ctx, dukky_html_menu_element_label_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_label_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Add read/write property */
	duk_dup(ctx, 0);
	duk_push_string(ctx, "compact");
	duk_push_c_function(ctx, dukky_html_menu_element_compact_getter, 0);
	duk_push_c_function(ctx, dukky_html_menu_element_compact_setter, 1);
	duk_def_prop(ctx, -4, DUK_DEFPROP_HAVE_GETTER |
		DUK_DEFPROP_HAVE_SETTER |
		DUK_DEFPROP_HAVE_ENUMERABLE | DUK_DEFPROP_ENUMERABLE |
		DUK_DEFPROP_HAVE_CONFIGURABLE);
	duk_pop(ctx);

	/* Set the destructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_html_menu_element___destructor, 1);
	duk_set_finalizer(ctx, -2);
	duk_pop(ctx);

	/* Set the constructor */
	duk_dup(ctx, 0);
	duk_push_c_function(ctx, dukky_html_menu_element___constructor, 2);
	duk_put_prop_string(ctx, -2, "\xFF\xFFNETSURF_DUKTAPE_INIT");
	duk_pop(ctx);

	return 1; /* The prototype object */
}
static duk_ret_t RigidBody_Get_NewPhysicsCollision(duk_context* ctx)
{
    RigidBody* thisObj = GetThisWeakObject<RigidBody>(ctx);
    SignalWrapper_RigidBody_NewPhysicsCollision* wrapper = new SignalWrapper_RigidBody_NewPhysicsCollision(thisObj, &thisObj->NewPhysicsCollision);
    PushValueObject(ctx, wrapper, SignalWrapper_RigidBody_NewPhysicsCollision_ID, SignalWrapper_RigidBody_NewPhysicsCollision_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_RigidBody_NewPhysicsCollision_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_RigidBody_NewPhysicsCollision_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_RigidBody_NewPhysicsCollision_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_RigidBody_NewPhysicsCollision_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_RigidBody_NewPhysicsCollision_Emit, 5);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
static duk_ret_t IAsset_Get_PropertyStatusChanged(duk_context* ctx)
{
    IAsset* thisObj = GetThisWeakObject<IAsset>(ctx);
    SignalWrapper_IAsset_PropertyStatusChanged* wrapper = new SignalWrapper_IAsset_PropertyStatusChanged(thisObj, &thisObj->PropertyStatusChanged);
    PushValueObject(ctx, wrapper, SignalWrapper_IAsset_PropertyStatusChanged_ID, SignalWrapper_IAsset_PropertyStatusChanged_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_IAsset_PropertyStatusChanged_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_IAsset_PropertyStatusChanged_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_IAsset_PropertyStatusChanged_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_IAsset_PropertyStatusChanged_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_IAsset_PropertyStatusChanged_Emit, 1);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
static duk_ret_t Terrain_Get_ParentEntityAboutToBeDetached(duk_context* ctx)
{
    Terrain* thisObj = GetThisWeakObject<Terrain>(ctx);
    SignalWrapper_Terrain_ParentEntityAboutToBeDetached* wrapper = new SignalWrapper_Terrain_ParentEntityAboutToBeDetached(thisObj, &thisObj->ParentEntityAboutToBeDetached);
    PushValueObject(ctx, wrapper, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_ID, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Finalizer, false);
    duk_push_c_function(ctx, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Connect");
    duk_push_c_function(ctx, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Connect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "connect");
    duk_push_c_function(ctx, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "Disconnect");
    duk_push_c_function(ctx, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Disconnect, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "disconnect");
    duk_push_c_function(ctx, SignalWrapper_Terrain_ParentEntityAboutToBeDetached_Emit, 0);
    duk_put_prop_string(ctx, -2, "Emit");
    return 1;
}
 void js_init_require(JSVM* vm)
 {
     duk_context* ctx = vm->GetJSContext();
     duk_get_global_string(ctx, "Duktape");
     duk_push_c_function(ctx, js_module_search, 4);
     duk_put_prop_string(ctx, -2, "modSearch");
     duk_pop(ctx);
 }
Example #21
0
static void duk__console_reg_vararg_func(duk_context *ctx, duk_c_function func, const char *name, duk_uint_t flags) {
	duk_push_c_function(ctx, func, DUK_VARARGS);
	duk_push_string(ctx, "name");
	duk_push_string(ctx, name);
	duk_def_prop(ctx, -3, DUK_DEFPROP_HAVE_VALUE | DUK_DEFPROP_FORCE);  /* Improve stacktraces by displaying function name */
	duk_set_magic(ctx, -1, (duk_int_t) flags);
	duk_put_prop_string(ctx, -2, name);
}
Example #22
0
File: vm.c Project: cjihrig/sjs
static void sjs__setup_modsearch(sjs_vm_t* vm) {
    duk_context* ctx = vm->ctx;

    duk_get_global_string(ctx, "Duktape");
    duk_push_c_function(ctx, sjs__modsearch, 4 /* nargs */);
    duk_put_prop_string(ctx, -2, "modSearch");
    duk_pop(ctx);
}
Example #23
0
File: main.c Project: dzzie/duk4vb
void RegisterNativeHandlers(duk_context *ctx){
	
	duk_push_global_object(ctx);
	duk_push_c_function(ctx, comResolver, DUK_VARARGS);
	duk_put_prop_string(ctx, -2, "resolver");
	duk_pop(ctx);  /* pop global */

	duk_push_global_object(ctx);
	duk_push_c_function(ctx, prompt, 2);
	duk_put_prop_string(ctx, -2, "prompt");
	duk_pop(ctx);  /* pop global */

	duk_push_global_object(ctx);
	duk_push_c_function(ctx, my_alert, 2);
	duk_put_prop_string(ctx, -2, "alert");
	duk_pop(ctx);  /* pop global */

	//duk_eval_string(ctx, "Duktape.Logger.clog.l = 0;");duk_pop(ctx); //change log level
	duk_eval_string(ctx, "Duktape.Logger.prototype.raw = print");
	duk_pop(ctx);

	/* ReferenceError: does not include a line number
	   SyntaxError: always include line number.
	   err type is not part of err.message, (line is not part of err.message either not sure how to detect type, but 
	   easy answer is to just always add it for standardization... but sometimes we get a 2x display bug with this code.. 
	   this is easy enough..we will just detect it in the ocx and strip it.
	*/
	duk_eval_string(ctx,"Duktape.errCreate = function (err) "
		              " {\n"  
					  "  try { \n"
					  "   if (typeof err === 'object' &&\n"
					  "       typeof err.message !== 'undefined' &&\n"
					  "        typeof err.lineNumber === 'number')"
	                  "      { \n"
					  "        //alert(err.message.indexOf('ReferenceError:')+' '+err.message);\n"
					  "		   err.message = err.message + ' (line ' + err.lineNumber + ')';\n"
					  "      } \n"
					  "  } catch (e) { \n"
					  "    // ignore; for cases such as where message is not writable etc\n"
					  "  } \n"
					  "  return err;\n"
					  " }");
	duk_pop(ctx);   

}
void jsapi_init_graphics(JSVM* vm)
{
    duk_context* ctx = vm->GetJSContext();

    js_class_get_prototype(ctx, "Atomic", "Light");
    duk_push_c_function(ctx, Light_SetShadowCascade, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "setShadowCascade");
    duk_push_c_function(ctx, Light_SetShadowCascadeParameter, 2);
    duk_put_prop_string(ctx, -2, "setShadowCascadeParameter");
    duk_push_c_function(ctx, Light_GetShadowCascade, 0);
    duk_put_prop_string(ctx, -2, "getShadowCascade");
    duk_push_c_function(ctx, Light_SetShadowBias, 2);
    duk_put_prop_string(ctx, -2, "setShadowBias");
    duk_pop(ctx);

    js_class_get_prototype(ctx, "Atomic", "Material");
    duk_push_c_function(ctx, Material_GetShaderParameters, 0);
    duk_put_prop_string(ctx, -2, "getShaderParameters");
    duk_push_c_function(ctx, Material_SetShaderParameter, 2);
    duk_put_prop_string(ctx, -2, "setShaderParameter");
    duk_pop(ctx);

    // static methods
    js_class_get_constructor(ctx, "Atomic", "Material");
    duk_push_c_function(ctx, Material_GetTextureUnitName, 1);
    duk_put_prop_string(ctx, -2, "getTextureUnitName");
    duk_pop(ctx);

}
Example #25
0
void JsLibrarian::Register(duk_context* duktapeContext)
{
   duk_push_global_object(duktapeContext);

   duk_push_c_function(duktapeContext, JsLibrarian::Constructor, DUK_VARARGS);
   duk_put_prop_string(duktapeContext, -2, "Librarian");

   duk_pop(duktapeContext);
}
Example #26
0
duk_ret_t dukopen_nsp_texture(duk_context *ctx) {
    duk_idx_t idx = duk_push_object(ctx);
    duk_idx_t stats_constr = duk_push_c_function(ctx, nsp_texture_constructor, DUK_VARARGS);
    duk_idx_t stats_prot = duk_push_object(ctx);
    duk_put_function_list(ctx, stats_prot, nsp_texture_methods);
    duk_put_prop_string(ctx, stats_constr, "prototype");
    duk_put_prop_string(ctx, idx, "Texture");
    return 1;
}
void Expose_EntityReference(duk_context* ctx)
{
    duk_push_c_function(ctx, EntityReference_Ctor_Selector, DUK_VARARGS);
    duk_push_object(ctx);
    duk_put_function_list(ctx, -1, EntityReference_Functions);
    DefineProperty(ctx, "ref", EntityReference_Get_ref, EntityReference_Set_ref);
    duk_put_prop_string(ctx, -2, "prototype");
    duk_put_global_string(ctx, EntityReference_ID);
}
void jsapi_init_graphics(JSVM* vm)
{
    duk_context* ctx = vm->GetJSContext();

    js_class_get_prototype(ctx, "Atomic", "Light");
    duk_push_c_function(ctx, Light_SetShadowCascade, DUK_VARARGS);
    duk_put_prop_string(ctx, -2, "setShadowCascade");
    duk_push_c_function(ctx, Light_SetShadowCascadeParameter, 2);
    duk_put_prop_string(ctx, -2, "setShadowCascadeParameter");
    duk_push_c_function(ctx, Light_GetShadowCascade, 0);
    duk_put_prop_string(ctx, -2, "getShadowCascade");
    duk_push_c_function(ctx, Light_SetShadowBias, 2);
    duk_put_prop_string(ctx, -2, "setShadowBias");
    duk_pop(ctx);

    js_class_get_prototype(ctx, "Atomic", "Material");
    duk_push_c_function(ctx, Material_GetShaderParameters, 0);
    duk_put_prop_string(ctx, -2, "getShaderParameters");
    duk_push_c_function(ctx, Material_SetShaderParameter, 2);
    duk_put_prop_string(ctx, -2, "setShaderParameter");
    duk_pop(ctx);

    js_class_get_prototype(ctx, "Atomic", "Octree");
    duk_push_c_function(ctx, Octree_Raycast, DUK_VARARGS);
    duk_set_magic(ctx, -1, (unsigned) DUK_MAGIC_RAYCAST);
    duk_put_prop_string(ctx, -2, "rayCast");
    duk_push_c_function(ctx, Octree_Raycast, DUK_VARARGS);
    duk_set_magic(ctx, -1, (unsigned) DUK_MAGIC_RAYCAST_SINGLE);
    duk_put_prop_string(ctx, -2, "rayCastSingle");
    duk_pop(ctx);

    js_class_get_prototype(ctx, "Atomic", "Camera");
    duk_push_c_function(ctx, Camera_GetScreenRay, 2);
    duk_put_prop_string(ctx, -2, "getScreenRay");
    duk_pop(ctx);

    // static methods
    js_class_get_constructor(ctx, "Atomic", "Material");
    duk_push_c_function(ctx, Material_GetTextureUnitName, 1);
    duk_put_prop_string(ctx, -2, "getTextureUnitName");
    duk_pop(ctx);


}
void PushWeakObject(duk_context* ctx, Object* object)
{
    if (!object)
    {
        duk_push_null(ctx);
        return;
    }

    duk_push_heap_stash(ctx);

    // Check if the wrapper for the object already exists in stash
    // This is required so that comparisons of object references (e.g. against the me property) work properly
    if (duk_has_prop_index(ctx, -1, (size_t)object))
    {
        duk_get_prop_index(ctx, -1, (size_t)object);
        WeakPtr<Object>* oldPtr = GetWeakPtr(ctx, -1);
        if (oldPtr && oldPtr->Get() == object)
        {
            duk_remove(ctx, -2); // Remove stash
            return;
        }
        else
            duk_pop(ctx); // Valid existing wrapper not found
    }

    duk_push_object(ctx);
    WeakPtr<Object>* ptr = new WeakPtr<Object>(object);
    duk_push_pointer(ctx, ptr);
    duk_put_prop_string(ctx, -2, "\xff""weak");
    duk_push_c_function(ctx, WeakPtr_Finalizer, 1);
    duk_set_finalizer(ctx, -2);

    // Set prototype. If not found, use base class prototype (e.g. IComponent)
    duk_get_global_string(ctx, object->GetTypeName().CString());
    if (!duk_is_object(ctx, -1))
    {
        duk_pop(ctx);
        duk_get_global_string(ctx, object->GetTypeInfo()->GetBaseTypeInfo()->GetTypeName().CString());
    }
    duk_get_prop_string(ctx, -1, "prototype");
    duk_set_prototype(ctx, -3);
    duk_pop(ctx);

    // Proxied property access handling for scene, entity & component
    if (object->GetType() == Scene::GetTypeStatic())
        SetupProxy(ctx, SceneProxyFunctions);
    if (object->GetType() == Entity::GetTypeStatic())
        SetupProxy(ctx, EntityProxyFunctions);
    else if (dynamic_cast<IComponent*>(object))
        SetupProxy(ctx, ComponentProxyFunctions);

    // Store to stash
    duk_dup(ctx, -1);
    duk_put_prop_index(ctx, -3, (size_t)object);
    duk_remove(ctx, -2); // Remove stash
}
static duk_ret_t test_1(duk_context *ctx, void *udata) {
	(void) udata;

	duk_eval_string_noresult(ctx,
		"this.myFinalizer = function myfin(o) {\n"
		"    print('finalizer, rescuing object', JSON.stringify(o));\n"
		"    var global = new Function('return this')();\n"
		"    global.rescued = o;\n"
		"}\n"
	);

	/* First call; object gets finalized when call returns and call
	 * handling does a duk_set_top() call.  The object is rescued and
	 * later reused.
	 */
	duk_push_c_function(ctx, my_func, 3 /*nargs*/);
	duk_push_null(ctx);
	duk_eval_string(ctx,
		"(function () {\n"
		"    var res = { name: 'my object' };\n"
		"    Duktape.fin(res, myFinalizer);\n"
		"    return res;\n"
		"})()\n");
	duk_push_null(ctx);
	printf("calling\n");
	duk_call(ctx, 3);
	printf("returned\n");
	duk_pop(ctx);

	/* Object is reused for another call, and rescued again. */
	duk_push_c_function(ctx, my_func, 3 /*nargs*/);
	duk_push_null(ctx);
	duk_eval_string(ctx, "rescued");
	duk_eval_string_noresult(ctx, "rescued = null;");
	duk_push_null(ctx);
	printf("calling\n");
	duk_call(ctx, 3);
	printf("returned\n");
	duk_pop(ctx);

	printf("final top: %ld\n", (long) duk_get_top(ctx));
	return 0;
}