Server Side JavaScript: O3


Published on

Server-Side Javascript (SSJS) has been creating a lot of buzz lately, and it's not hard to see why. We have just begun to discover the possibilities opened up by running Javascript on the server, and it's something we're very excited about. That is why during this presentation we will introduce O3. O3 is a set of C++ components, which are exposed through a Javascript API, and can be used anywhere, be it in a browser or on a server... on all major operating systems. What's more, O3 also provides a set of libraries which allows you to write your own C++ components, and expose them through a Javascript API as well. Eddy and Mike of will show you how to use O3 within a simple collaborative application.

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

Server Side JavaScript: O3

  1. 2. <ul><li>Our demo: </li></ul><ul><ul><li>Real-time c ollaborative city planner </li></ul></ul><ul><ul><li>Changes are distributed using XMPP </li></ul></ul><ul><ul><li>XMPP bot updates traffic patterns </li></ul></ul><ul><ul><li>Runs in Javascript on the server </li></ul></ul>Welcome!
  2. 3. What is O 3 ? <ul><li>Collection of C++ components, made available through Javascript API </li></ul><ul><li>Runs on all major browsers, and all major operating systems </li></ul><ul><li>Library to create scriptable C++ components </li></ul>
  3. 4. What is O 3 ? <ul><li>Comes in two flavors: </li></ul><ul><ul><li>O 3 client-side: </li></ul></ul><ul><ul><ul><li>Runs as plug-in in the browser </li></ul></ul></ul><ul><ul><ul><li>Extends DOM API with O3 components </li></ul></ul></ul><ul><ul><ul><li>Useful if you’re developing in the browser </li></ul></ul></ul><ul><ul><li>O 3 server-side: </li></ul></ul><ul><ul><ul><li>Runs as standalone application </li></ul></ul></ul><ul><ul><ul><li>Uses Google’s V8 engine to run scripts </li></ul></ul></ul><ul><ul><ul><li>Provides the same components </li></ul></ul></ul>
  4. 5. What is O 3 ? <ul><li>All O3 components are available on both the client and the server! </li></ul><ul><li>To use O3 at the client, you need a plugin however… </li></ul>
  5. 6. What is O3? <ul><li>O3 Library consists of three parts: </li></ul><ul><ul><li>Component model </li></ul></ul><ul><ul><li>Reflection layer </li></ul></ul><ul><ul><li>Containers, algorithms, etc. </li></ul></ul>
  6. 7. Component model <ul><li>Subset of Microsoft’s COM specification (as opposed to XPCOM, CORBA, etc.) </li></ul><ul><li>Looks like this: o3_iid(iFoo, 0x3F2504E0, 0x4F89, 0x11D3, 0x9A, 0x0C, 0x03, 0x05, 0xE8, 0x2C, 0x33, 0x01); struct iFoo : iUnk { virtual void bar(int x) = 0; }; o3_cls(cFoo) struct cFoo : cUnk, iFoo { o3_begin_class(cUnk) o3_add_iface(iFoo) o3_end() }; </li></ul>
  7. 8. Reflection layer <ul><ul><li>Special interface: struct iScr : iUnk { int resolve(const char*); void invoke(iCtx* ctx, Access access, int id, int argc, Var* argv, Var* rval, Ex* ex); … }; </li></ul></ul><ul><ul><li>Allows you to query C++ objects for their methods and properties! </li></ul></ul><ul><ul><li>Indexed based because some script engines require this (i.e. IDispatch) </li></ul></ul>
  8. 9. Containers, algorithms, etc. <ul><ul><li>We don’t use STL (although you could!) </li></ul></ul><ul><ul><li>Our containers have lots of useful features and optimizations: </li></ul></ul><ul><ul><ul><li>Stack allocation for small strings </li></ul></ul></ul><ul><ul><ul><li>Copy-on write semantics </li></ul></ul></ul><ul><ul><ul><li>Automatic UTF-8/UTF-16 conversion (not possible with STL) </li></ul></ul></ul><ul><ul><ul><li>Vectors are scriptable as well! (using Array interface) </li></ul></ul></ul>
  9. 10. Writing components <ul><li>Writing O3 components is easy! </li></ul><ul><ul><li>All you need to do is add some tags: o3_get int value(); // getter o3_set int setValue(int value); // setter o3_fun int function(int x); // function </li></ul></ul><ul><ul><li>Code generator will do the rest! (also written in Javascript) </li></ul></ul>
  10. 11. Writing components <ul><li>We want our components to be as fast as possible! </li></ul><ul><li>Immediate properties: o3_imm_get int foo; // read-only o3_imm_set int bar; // write-only o3_imm int value; // read/write </li></ul><ul><li>No getters/setters are generated, properties are accessed directly. </li></ul>
  11. 12. Writing components <ul><li>We want to be able to load components on the fly! </li></ul><ul><li>Extension properties: </li></ul><ul><ul><li>In C++: o3_ext(“cO3”) o3_fun static int test(int x); </li></ul></ul><ul><ul><li>In Javascript: o3.loadModule(“test”); o3.test(3); </li></ul></ul>
  12. 13. Dealing with async <ul><li>Use threads to implement asynchronous Javascript calls. </li></ul><ul><li>Use message passing to implement callbacks </li></ul><ul><li>This requires an explicit message loop on the server: while (true) o3.wait(); </li></ul><ul><li>Not needed at the client! (We can hook into the browser message loop there…) </li></ul>
  13. 14. HTTP Component <ul><li>Based on XMLHttpRequest API </li></ul><ul><li>Example: var http = o3.http(); http.onprogress = function(http) { if (http.bytesReceived > 10000) … </li></ul><ul><li>} http.onreadystatechange = function(http) { if (http.readystate == http.COMPLETED) … }“GET”, “”); http.send(); </li></ul>
  14. 15. XML Component <ul><li>Based on XML DOM API </li></ul><ul><li>Example: var xml = o3.xml(); var doc = xml.parseFromString(string); var element = doc.documentElement; for (var child = element.firstChild; child != element.lastChild; child = child.nextSibling) … </li></ul>
  15. 16. FS Component <ul><li>Example: var cwd = o3.cwd; var file = cwd.get(“test.txt”); file.onchange = function(file) { file.copy(cwd.get(“test.bak”)); } </li></ul><ul><li>We’re working on other components as well! </li></ul><ul><ul><li>IMAGE </li></ul></ul><ul><ul><li>SOCKET </li></ul></ul><ul><ul><li>JS </li></ul></ul>
  16. 17. Putting it together <ul><li>Wait for update from XMPP server (using HTTP) </li></ul><ul><li>Parse XMPP data (using XML) </li></ul><ul><li>Calculate traffic patterns (using trip generation) </li></ul><ul><li>Notify server of changes (using HTTP) </li></ul>
  17. 18. Thank you!