Lightweight Languages Workshop November 2002 © 2002 IBM Corporation IBM Lightweight Services Implementing persistent server-side JavaScript Christopher Vincent
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 2 Agenda Scenario Goals Compromises Conclusions
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 3 Scenario: script-based components Metadata configures environment Address space per instance
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 4 Goals Robust code, minimal effort High-level, dynamic environment Patch live code, interactive debugging (REPL) Asynchronous / event-driven APIs Application types Avoid deadlock, cancel operations Transparent persistence, transactions Program state rollbacks Survive container restart / failure
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 5 Simple JavaScript example No arbitrary serialization Only checkpoint when idle Single-threaded Event-handlers provide predictable boundaries var fib1 = 0; var fib2 = 1; function nextFibonacci() { var fib3 = fib1 + fib2; fib1 = fib2; fib2 = fib3; return fib3; }
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 6 Non-transactional resources Managed access to the non-transactional world Queue operations until event-handler / transaction succeeds Script APIs wrapper message-passing var session = new InstantMessaging.Session(); session.onMessage = onMessage; session.login(im.ibm.com, userId, password); function onMessage(session, from, text) { session.send(from, You said: + text);... throw new Error(darn.); }
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 7 Container restart / failure Complete transparency impractical Consider late failures vs. explicit resume Minimize effort to restore resources var session =... session.login(im.ibm.com, userId, password); Host.onResume = onResume; function onResume() { session.login(); }
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 8 Conclusions Practical results Real-world examples in lines of JavaScript Implementing the container is hard Underlying message-passing system, resource management Custom-tailored extension APIs Applies to other languages with object serialization
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 9 More information IBM Lightweight Services Introduction to IBM Lightweight Services Extending J2EE with script-based agents Mozilla Rhino JavaScript engine ECMAScript language specification
LL2: IBM Lightweight Services 9 November 2002 | © 2002 IBM Corporation 10 Changes to environment Discard changes to built-in objects Simplifies serializer, saves space Could detect modifications Array.prototype.peek = function () { return this[this.length – 1]; }; x = [1, 2, 3]; x.peek(); var re = /foo(\d+)bar/i; re.exec(foo123bar); var match = RegExp.$1;