Print this page
XXX keith II

Split Close
Expand all
Collapse all
          --- old/./README.md
          +++ new/./README.md
↓ open down ↓ 555 lines elided ↑ open up ↑
 556  556                  my_result_t *rp = ...;
 557  557  
 558  558                  /*
 559  559                   * In thread pool context -- do not call any of the
 560  560                   * following functions:
 561  561                   * v8plus_obj_hold()
 562  562                   * v8plus_obj_rele()
 563  563                   * v8plus_jsfunc_hold()
 564  564                   * v8plus_jsfunc_rele()
 565  565                   * v8plus_call()
 566      -                 * v8plus_method_call()
      566 +                 * v8plus_method_call_direct()
 567  567                   * v8plus_defer()
 568  568                   *
 569  569                   * If you touch anything inside op, you may need locking to
 570  570                   * protect against functions called in the main thread.
 571  571                   */
 572  572                  ...
 573  573  
 574  574                  return (rp);
 575  575          }
 576  576  
↓ open down ↓ 67 lines elided ↑ open up ↑
 644  644                  this._native = binding._create.apply(this,
 645  645                      Array.prototype.slice.call(arguments));
 646  646                  this._native._emit = function () {
 647  647                          var args = Array.prototype.slice.call(arguments);
 648  648                          self.emit.apply(self, args);
 649  649                  };
 650  650          }
 651  651          util.inherits(MyObjectWrapper, events.EventEmitter);
 652  652  
 653  653  Then, in C code, you must arrange for libuv to call a C function in the
 654      -context of the main event loop.  How to do this depends on what type of
 655      -asynchronous event you are waiting for; for example, the `uv_poll` mechanism
 656      -will call you back in the appropriate context when a file descriptor becomes
 657      -readable or writable.  Because libuv is a C library, you can easily use
 658      -these interfaces directly in your v8+ addon.  Your libuv callback might then
 659      -contain code looking something like this:
      654 +context of the main event loop.  The function `v8plus_method_call()` is safe
      655 +to call from any thread: depending on the context in which it is invoked, it
      656 +will either make the call directly or queue the call in the main event loop
      657 +and block on a reply.  Simply arrange to call back into your JavaScript
      658 +object when you wish to post an event:
 660  659  
 661  660          nvlist_t *eap;
 662  661          nvlist_t *erp;
 663  662          my_object_t *op = ...;
 664  663          ...
 665  664          eap = v8plus_obj(
 666  665              V8PLUS_TYPE_STRING, "0", "my_event",
 667  666              ...,
 668  667              V8PLUS_TYPE_NONE);
 669  668  
↓ open down ↓ 51 lines elided ↑ open up ↑
 721  720  functions that provide methods.
 722  721  
 723  722  ### nvlist_t *v8plus_method_call(void *op, const char *name, const nvlist_t *ap)
 724  723  
 725  724  Calls the method named by `name` in the native object `op` with encoded
 726  725  argument list `ap`.  The method must exist and must be a JavaScript
 727  726  function.  Such functions may be attached by JavaScript code as in the event
 728  727  emitter example above.  The effects of using this function to call a native
 729  728  method are undefined.
 730  729  
      730 +As JavaScript functions must be called from the event loop thread,
      731 +`v8plus_method_call()` contains logic to determine whether we are in the
      732 +correct context or not.  If we are running on some other thread we will
      733 +queue the request and sleep, waiting for the event loop thread to make the
      734 +call.  In the simple case, where we are already in the correct thread, we
      735 +make the call directly.
      736 +
 731  737  ## FAQ
 732  738  
 733  739  - Why?
 734  740  
 735  741  Because C++ is garbage.  Writing good software is challenging enough without
 736  742  trying to understand a bunch of implicit side effects or typing templated
 737  743  identifiers that can't fit in 80 columns without falling afoul of the
 738  744  language's ambiguous grammar.  Don't get me started.
 739  745  
 740  746  - Why not use [FFI](https://github.com/rbranson/node-ffi)?
↓ open down ↓ 112 lines elided ↑ open up ↑
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX