Share
Embed
Annual Pro sale · Save $30
Have presentations of your own?
Share with private links, password protection, and more control over your work.
Transcript
152 slides
-
How to Use libr3 ˖ $SFBUFUSFF ˖ *OTFSUSPVUFXJUIEBUB ˖ $PNQJMFUSFF
˖ .BUDISPVUFBOEHFUUIFEBUB -
Sample 1/2 n = r3_tree_create(10);! ! int route_data = 3;!
! r3_tree_insert_routel(n, METHOD_GET, "/blog/post", ! sizeof("/blog/post") - 1, &route_data );! ! int err = r3_tree_compile(n, &errstr); -
Sample 1/2 n = r3_tree_create(10);! ! int route_data = 3;!
! r3_tree_insert_routel(n, METHOD_GET, "/blog/post", ! sizeof("/blog/post") - 1, &route_data );! ! int err = r3_tree_compile(n, &errstr); Create Tree -
Sample 1/2 n = r3_tree_create(10);! ! int route_data = 3;!
! r3_tree_insert_routel(n, METHOD_GET, "/blog/post", ! sizeof("/blog/post") - 1, &route_data );! ! int err = r3_tree_compile(n, &errstr); Insert Route -
Sample 1/2 n = r3_tree_create(10);! ! int route_data = 3;!
! r3_tree_insert_routel(n, METHOD_GET, "/blog/post", ! sizeof("/blog/post") - 1, &route_data );! ! int err = r3_tree_compile(n, &errstr); Data -
Sample 1/2 n = r3_tree_create(10);! ! int route_data = 3;!
! r3_tree_insert_routel(n, METHOD_GET, "/blog/post", ! sizeof("/blog/post") - 1, &route_data );! ! int err = r3_tree_compile(n, &errstr); Compile -
Sample 2/2 match_entry * entry = match_entry_create("/blog/post");! entry->request_method = METHOD_GET;!
! route *matched_route = r3_tree_match_route(n, entry);! matched_route->data; // The Data! ! match_entry_free(entry);! r3_tree_free(n); -
Sample 2/2 match_entry * entry = match_entry_create("/blog/post");! entry->request_method = METHOD_GET;!
! route *matched_route = r3_tree_match_route(n, entry);! matched_route->data; // The Data! ! match_entry_free(entry);! r3_tree_free(n); Create Entry -
Sample 2/2 match_entry * entry = match_entry_create("/blog/post");! entry->request_method = METHOD_GET;!
! route *matched_route = r3_tree_match_route(n, entry);! matched_route->data; // The Data! ! match_entry_free(entry);! r3_tree_free(n); Match Route -
Sample 2/2 match_entry * entry = match_entry_create("/blog/post");! entry->request_method = METHOD_GET;!
! route *matched_route = r3_tree_match_route(n, entry);! matched_route->data; // The Data! ! match_entry_free(entry);! r3_tree_free(n); Route Data -
Sample 2/2 match_entry * entry = match_entry_create(“/blog/post/1/2/3“);! entry->request_method = METHOD_GET;
// Method Condition! ! route *matched_route = r3_tree_match_route(n, entry);! ! matched_route->data; // The Data! entry->vars; //Captured Vars, a string array! ! r3_tree_free(n); -
__cdecl val = sumExample(2,3) ; // push arguments to the
stack, from right to left! push 3 ! push 2 ! ! ; // call the function! call _sumExample ! ! ; // cleanup the stack by adding the size of the arguments to! ; // ESP register! add esp,8 ! ! ; // copy the return value from EAX to a local variable (int c)! mov dword ptr [c],eax -
So node-ffi Knows ˖ )PXUPDBMMGVODUJPOJO$$ EZOBNJDMJC ˖ )PXUPDPOWFSU+BWB4DSJQUEBUBUP$$ EBUB ˖
)PXUPTFOEQBSBNFUFS ˖ )PXUPHSBCSFUVSOWBMVF -
@TooTallNate ˖ /BUIBO3BKMJDI ˖ /PEFKTDPSFDPNNJUUFS ˖ 0OFBVUIPSPG/PEFKTJO"DUJPO ˖ -PUTPGSFMBUFEQBDLBHF ˖
/PE0CK$ /PEFKT̠0CKFDUJWF$CSJEHF -
ffi Foreign Function Call ref Reference Tools ref-struct Struct Helper
ref-array Array Helper -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 Library name! Use dlopen -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 A function named ‘ceil’ return double Take one double parameter -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 return type -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 arguments type -
Types void bool byte pointer (u)short (u)int (u)int16 (u)int32 (u)int64
(u)longlong size_t float (u)char string object -
Example var ffi = require('ffi');! ! var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! });! ! libm.ceil(1.5); // 2 Call ceil -
math.h http://www.cplusplus.com/reference/cmath/ceil/ double ceil (double x); var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! }); -
math.h http://www.cplusplus.com/reference/cmath/ceil/ double ceil (double x); var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! }); -
math.h http://www.cplusplus.com/reference/cmath/ceil/ double ceil (double x); var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! }); -
math.h http://www.cplusplus.com/reference/cmath/ceil/ double ceil (double x); var libm = ffi.Library('libm',
{! 'ceil': [ 'double', [ 'double' ] ]! }); -
struct _edge {! char * pattern; // 8 bytes! node
* child; // 8 bytes! unsigned char pattern_len; // 1 byte! unsigned char opcode:4; // 4 bit! unsigned char has_slug:1; // 1 bit! }; var StructType = require('ref-struct');! ! var edge = StructType({! pattern: "string",! child: "pointer",! pattern_len: "ushort",! opcode: "uchar",! has_slug: "uchar"! }); -
ref-struct ˖ $SFBUF"#*DPNQMJBOUTUSVDUJOTUBODFTPOUPQPG #VFST ˖ "#* "QQMJDBUJPO#JOBSZ*OUFSGBDF ˖ UIFTJ[FT
MBZPVU BOEBMJHONFOUPGEBUBUZQFT ˖ https://en.wikipedia.org/wiki/Application_binary_interface -
typedef struct {! str_array * vars;! const char * path;
// current path to dispatch! int path_len; // the length of the current path! int request_method; // current request method! void * data; // route ptr! char * host; // the request host! ! ... -
typedef struct {! str_array * vars;! const char * path;
// current path to dispatch! int path_len; // the length of the current path! int request_method; // current request method! void * data; // route ptr! char * host; // the request host! ! ... var ref = require('ref');! ! var str_array = StructType({ ... });! ! var match_entry = StructType({! vars: ref.refType(str_array),! path: "string",! path_len: "int",! request_method: "int",! data: "pointer",! host: "pointer",! ... -
ref.refType StructType({! var1: str_array,! var2: ref.refType(str_array),! var3: ref.refType(ref.refType(str_array)),! }); str_array
var1;! str_array * var2;! str_array **var3; -
#define r3_tree_insert_path(n,p,d) r3_tree_insert_pathl_ex(n,p,s var libr3 = ffi.Library('libr3', {! "r3_tree_insert_pathl_ex": ["pointer",
["pointer", "string", " });! ! ! var r3_tree_insert_path = function (tree, path, data) {! return libr3.r3_tree_insert_pathl_ex(tree, path, path.length, }; -
node-ffi-generate ˖ "VUPNBUJDHFOFSBUFEFOJUJPOGSPNIMF ˖ $POT ˖ 3FTVMUJTUPPEFUBJM UPPCJH ˖ .JHIUOPUTVJUGPSVTF
˖ EFOF https://github.com/tjfontaine/node-ffi-generate -
node-r3 ˖ 0OMZEFOFOFDFTTBSZNFUIPET ˖ NFUIPET ˖ 5SFFDSFBUF DPNQJMF EVNQ NBUDI
GSFF ˖ *OTFSUSPVUF QBUI ˖ .BUDI@FOUSZDSFBUF GSFF -
route * r3_tree_insert_routel(node *tree, int method, const char *path, int
path_len, void *data); -
route * r3_tree_insert_routel(node *tree, int method, const char *path, int
path_len, void *data); -
Example 1/2 var tree = libr3.r3_tree_create(10);! ! var data =
new Buffer(data_str + '\u0000');! ! r3_tree_insert_route(tree, 'GET','/path', data); -
Example 2/2 var node = libr3.r3_tree_match_route(tree, entry);! ! var data
= ref.deref(node).data; https://github.com/othree/node-r3/blob/46e29f72bbf68097ed66fd151f9ef42d5a3ede05/node-r3.js -
data = new Buffer(data + '\u0000');! r3_tree_insert_path(this.tree, route, data); var
node = r3_tree_match(this.tree, path, entry);! ! var data = ref.readCString(node.deref().data, 0); -
var i = this.i++;! this.data[i] = data;! ! var iref
= ref.alloc('int', i);! ! libr3.r3_tree_insert_route(this.tree, method, route, iref); var node = libr3.r3_tree_match_route(this.tree, entry);! ! var index = node.deref().data.reinterpret(4).readUInt32LE(0);! ! var data = this.data[index]; -
var i = this.i++;! this.data[i] = data;! ! var iref
= ref.alloc('int', i);! ! libr3.r3_tree_insert_route(this.tree, method, route, iref); var node = libr3.r3_tree_match_route(this.tree, entry);! ! var index = node.deref().data.reinterpret(4).readUInt32LE(0);! ! var data = this.data[index]; Store Data -
var i = this.i++;! this.data[i] = data;! ! var iref
= ref.alloc('int', i);! ! libr3.r3_tree_insert_route(this.tree, method, route, iref); var node = libr3.r3_tree_match_route(this.tree, entry);! ! var index = node.deref().data.reinterpret(4).readUInt32LE(0);! ! var data = this.data[index]; Data for r3 -
var i = this.i++;! this.data[i] = data;! ! var iref
= ref.alloc('int', i);! ! libr3.r3_tree_insert_route(this.tree, method, route, iref); var node = libr3.r3_tree_match_route(this.tree, entry);! ! var index = node.deref().data.reinterpret(4).readUInt32LE(0);! ! var data = this.data[index]; Resize Buffer -
var i = this.i++;! this.data[i] = data;! ! var iref
= ref.alloc('int', i);! ! libr3.r3_tree_insert_route(this.tree, method, route, iref); var node = libr3.r3_tree_match_route(this.tree, entry);! ! var index = node.deref().data.reinterpret(4).readUInt32LE(0);! ! var data = this.data[index]; Read Index -
How About Foreign Calls ˖ 8IBUXFVTFE ˖ S@USFF@DSFBUF S@USFF@GSFF ˖
S@USFF@JOTFSU@QBUI ˖ S@USFF@DPNQJMF ˖ NBUDI@FOUSZ@DSFBUF NBUDI@FOUSZ@GSFF ˖ S@USFF@NBUDI@SPVUF -
Route req/s director ~2200 node-r3 0.0.1 ~1900 node-r3 0.0.3 ~5000
node-r3 0.1.0 7800~10600 Not Yet -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCase()];! if (!method) { throw new Error(route_frag[0]); }! r3_tree_insert_route(this.tree, method, route, data);! } else {! r3_tree_insert_route(this.tree, 0, route, data);! }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; WBSJBCMFT -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; DSFBUFUSFF -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; EBUBTUPSBHF -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; QBSTFSPVUFT -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; DSFBUFJOEFY -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; QBSTFTUSJOH -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; NFUIPEDPOEJUJPO -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; JOTFSUSPVUF -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCa if (!method) { throw new Error(route_fra r3_tree_insert_route(this.tree, method, } else {! r3_tree_insert_route(this.tree, 0, route }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; DPNQJMFUSFF -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCase()];! if (!method) { throw new Error(route_frag[0]); }! r3_tree_insert_route(this.tree, method, route, data);! } else {! r3_tree_insert_route(this.tree, 0, route, data);! }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; -
var Router = function (routes) {! var route, data, method,
route_frag, i = 0;! this.tree = libr3.r3_tree_create(10);! this.data = [];! this.index = [];! for (route in routes) {! this.data[i] = routes[route];! data = ref.alloc('int', i);! this.index[i] = data; // prevent GC! route = route.trim();! route_frag = route.split(' ');! if (route_frag.length > 1) {! route = route_frag[1];! method = METHODS[route_frag[0].toUpperCase()];! if (!method) { throw new Error(route_frag[0]); }! r3_tree_insert_route(this.tree, method, route, data);! } else {! r3_tree_insert_route(this.tree, 0, route, data);! }! i++;! }! libr3.r3_tree_compile(this.tree);! return this;! }; -
var Router = require('node-r3').Router;! ! var router = new Router({!
"/foo": "data string",! "/foo/bar": function () {},! "/foo/bar/qoo": {obj: 1},! });! ! var dispatched = router.match("/foo");! ! router.free(); -
var router = new Router({! "/": handler,! "/foo": fooHandler,! "/foo/{id}":
fooHandler,! "POST /me": postMeHandler,! "GET /me": getMeHandler,! "POST|GET /post": postHandler,! });! ! var server = http.createServer(! router.httpHandler(notfound)! );