Code Monkey home page Code Monkey logo

vm-titanium's Introduction

vm-titanium

node.js vm implementation for Titanium. This code is based heavily on James Halliday's vm-browserify, modified for creation of contexts within the Titanium runtime.

This is a work-in-progress; an experimental idea. This is still hacky/clunky, but I'm putting it here to get more eyes on it and see if others can divine more elegant solutions to the same problem. More specifically, anyone who can resolve any of the caveats below gets a digital high five.

install

$ npm install vm-titanium
$ cp ./node_modules/vm-titanium/vm-titanium.js /path/to/titanium_project/Resources
$ cp ./node_modules/vm-titanium/__context.js /path/to/titanium_project/Resources

supported on

So far this has only been tested on the following system, but it's likely to work in other configurations I just haven't tried yet. Log an issue if you have problems using it on any of Appcelerator's supported platforms.

  • TiSDK 3.3.0+
  • iOS 7.1
  • iPhone simulator

usage

var vm = require('vm-titanium');

// essentially just an eval
vm.runInThisContext('1 + 2 + 3', function(err, result) {
	console.log(result); // prints "6"
});

// change/add values from context
var context = vm.createContext({ foo: 'unchanged' });
vm.runInContext('foo="changed";newvalue=123', context, function(err, result) {
	console.log(context.foo);      // prints "changed"
	console.log(context.newvalue); // prints 123
});

// Create and open a Titanium Window. See issue #4 for explanation of why
// Ti and Titanium are manually added to the sandbox.
var sandbox = {
	Ti: Ti,
	Titanium: Titanium
};
var code =
'var win = Ti.UI.createWindow();' +
'win.add(Ti.UI.createView({' +
'  height: Ti.Platform.displayCaps.platformHeight/2,' +
'  backgroundColor: "#00f"' +
'});'
'win.open();'

vm.runInNewContext(code, sandbox, function(err, result) {
	// do other stuff in new context
});

testing

$ grunt

caveats

Not sure if these are surmountable, but take a look at the issue details if you're feeling brave.

  • [issue #2]: runInContext and runInNewContext require you to use a callback, making them unsuitable as drop-in replacements for node.js's synchronous implementations.
  • [issue #3]: Window created when creating a new context is visible, but shouldn't be.
  • [issue #4]: Certain Titanium namespaces always need to be manually copied into the context/sandbox when using runInContext or runInNewContext. (workaround in issue)

vm-titanium's People

Contributors

tonylukasavage avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.