mozilla
Your Search Results

    Callbacks

    この記事はまだ日本語に翻訳されていません。MDN の翻訳はボランティアによって行われています。是非 MDN に登録し、私たちの力になって下さい。

    C functions occasionally take function pointers as arguments, which are generally used as callbacks. In these cases, js-ctypes allows you can pass a regular JavaScript function as the callback. This is very powerful, since it allows native code to transparently call into JavaScript.

    Warning: Callbacks must be invoked on the same thread with which they were registered. There is no concurrency logic in js-ctypes, so invoking callbacks on other threads will cause things to crash.

    Creating callbacks

    Since callbacks are function pointers in C, js-ctypes has special handling for function pointer types. Consider the following code:

    function myJSCallback(foo, bar) { .... };
    var funcType = ctypes.FunctionType(...);
    var funcPtrType = funcType.ptr;
    var regularFuncPtr = funcPtrType();
    var callback = funcPtrType(myJSCallback); 
    

    js-ctypes detects that funcPtrType is a type of function pointer, and adds a special case to its constructor. In the ordinary case, invoking the type object creates a regular CData object containing a pointer to the data. However, if the first argument is a function, js-ctypes creates a special object (known internally as a CClosure) that wraps the JavaScript function within an ordinary C function. This can all be done in a single line of code, like so:

    var callback = ctypes.FunctionType(...).ptr(function(...) {...});
    
    Note: The use of .ptr() here isn't a method call; we're accessing a property that dynamically creates a callable object, and then invoking the result.

    If two arguments are passed to the callback constructor, the second is used as the this parameter:

    function myJSCallback() {
      alert(this.message);
    };
    
    var receiver = { message: 'hi there!' };
    var callback = funcPtrType(myJSCallback, receiver); // alerts with 'hi there' when the callback is invoked
    

    If three arguments are passed to the callback constructor, the third argument is used as a sentinel value which the callback returns if an exception is thrown. The sentinel value must be convertible to the return type of the callback:

    function myJSCallback() {
      throw "uh oh";
    };
    
    var callback1 = funcPtrType(myJSCallback, null, -1); // Returns -1 to the native caller when the exception is thrown.
    
    Warning: You must store a reference to the callback object as long as the native code might call it. If you don't, the GC might collect the relevant data structures, and the browser will crash when native code attempts to invoke your callback.

    Arguments and return values

    js-ctypes automatically handles the conversion between JavaScript and C value representations. Before the JavaScript callback function is invoked, js-ctypes converts the arguments passed by the caller to JavaScript values. Where possible, js-ctypes will convert arguments to primitive types. For arguments of complex types, temporary CData objects will be created.

    The return value is converted in a similar manner.

    ドキュメントのタグと貢献者

    Contributors to this page: Sheppy, arai, bholley, kscarfone
    最終更新者: arai,