On Github meltuhamy / native-calls-presentation
Problem 1:
JavaScript is great, but...
Remember this?
... and this?
Take the risk?
Insecure
Independent updates
Need to have them installed
Solution 1:
Compile native code to JavaScript
Solution 2:
Run native code in a sandboxed environment
It's a close one, but Native Client wins when we add threads
Problem 1 solved:
John McCutchan's
Problem 2:
because we need to talk to the browser
embed.postMessage({ command: 'sum', x: 1, y: 2 }); embed.addEventListener('message', function(message){ console.log(message); });
int sum(int x, int y){ return x + y; } void HandleMessage(pp::Var message){ // check which function is being called // get x and y from message int result = sum(x, y); pp::Var resultMessage(result); PostMessage(resultMessage); }
Solution:
Call C++ functions from JavaScript:
Module.sum(1, 2, function(result){ console.log(result); });
int sum(int x, int y){ return x + y; }
Just follow the spec
std::string vs char[]
std::vector<T> vs T[]
In WebIDL
dictionary MyDictionary { DOMString id; double value; }
In JavaScript
{"id": "myId", "value": 123.456}
In C++
typedef struct { std::string id; double value } MyDictionary;
Each interface name points to a map of functions
{ "MyInterface": { "foo": function(){...}, "bar": function(){...} }, "SecondInterface": { "fun": function(){...} } }
Each function is a class that implements call
class ServerStub_StepScene : public RPCServerStub{ public: virtual pp::Var call(const pp::VarArray* params, RPCError& error){ // unpack params // call concrete function // pack result } };
Using WebIDL, we know what paramaters the user expects
Extract parameters recursively using wrapper classes
pp::Var LongType::AsVar(const ValidType<int32_t>& v){ return pp::Var((int) v.getValue()); } ValidType<int32_t> LongType::Extract(const pp::Var& v){ if(v.is_int()){ // valid return ValidType<int32_t>((int32_t)v.AsInt()); } else { return ValidType<int32_t>(); // invalid } }
Total RPC time
Total C++ library time
% of time spent in C++ library
It saves a lot of time and effort.