Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: ability to persist context's between task runs #15

Open
ericallam opened this issue Apr 29, 2012 · 4 comments
Open

Feature: ability to persist context's between task runs #15

ericallam opened this issue Apr 29, 2012 · 4 comments

Comments

@ericallam
Copy link
Contributor

For the use case I am trying to use engine.js for It would be very helpful to be able to setup a context and then have multiple tasks run against that context. The use case (for me) is this:

A user submits code that somehow changes the context (let's say, incrementing a counter), does a console.log, and returns a certain value. I want to be able to verify three things:

  • The correct value was returned
  • The correct value was logged
  • The counter was incremented in the context

Currently, I can only verify the first two in the above list. I think to give the most flexibility and to limit the changes to the scope and feature set of engine.js, we could allow multiple tasks to run against a single context. The api might look something like this (assuming the api changes discussed in pull request 14:

var context = client.createContext("(function(locals) { var count=0; return { incr: function(){ count+=1; return count }}})");

var task = client.createTask();
task.setContext(context);
task.setLocals({});
task.setCode("incr()");

client.run(task, function(err, result, logs){
  console.log(result); // 1

  var verifyTask = client.createTask();
  verifyTask.setContext(context);
  verifyTask.setLocals({});
  verifyTask.setCode("incr()");

  client.run(verifyTask, function(err, result, logs){
    console.log(result); // 2
  });
});

Please let me know if this even sounds doable.

@rehanift
Copy link
Owner

It sounds like the goal is to be able to read back the context's globals after a task has run. Here is a sketch of how I can see this working with the existing API. Would this work for your use-case?

// File: counting_context.js /////////////////////////////////////////

(function(locals) { 

    return {
        count: 3,
        hello: "world",
        incr_global: function(){ 
            this.count+=1;
            return this.count;
        }
        ,incr_local: function(){
            locals.count++;
            return locals.count;
        }
    };
});


//////////////////////////////////////////////////////////////////////

var engine = require("engine.js").engine,
    client = engine.client.create(),
    task = client.createTask(),
    fs = require("fs");

task.setContext(fs.readFileSync("./counting_context.js","utf-8"));
task.setCode("incr_global() + incr_local();");
task.setLocals({count:5, foo:"bar"});

task.on("eval", function(last_eval, globals, locals){
    /*
     * last_eval: The last evaluated expression from the context
     *   ie. 10
     * 
     * globals: An object-literal of all global variables 
     *             (except functions) from the context, after the 
     *             task has finished
     *   ie. {
     *         count: 4,
     *         hello: "world"
     *       }
     * 
     * locals: An object-literal of all local variables from the 
     *             task-run, after the task has finished
     *   ie. {
     *         count: 6,
     *         foo: "bar"
     *       }
     * 
     */


});

task.run();

@ericallam
Copy link
Contributor Author

This would help a lot actually, and is probably much easier to implement than my suggestion. To move forward, should I just write up some pending specs, and document the feature?

@rehanift
Copy link
Owner

Ok. In preparation for this I started integrated contexify as the actual code execution strategy (rather than using the native vm module). I pushed my initial changes here d806a8d. All specs are passing, but I'm going to do some cleanup and refactoring.

If you could write a end-to-end test for this that would be a great way to get started.

@ericallam
Copy link
Contributor Author

Awesome, that looks pretty handy! I'll hopefully get some time tonight to write that end to end spec.

ericallam pushed a commit to ericallam/engine.js that referenced this issue Apr 30, 2012
ericallam pushed a commit to ericallam/engine.js that referenced this issue Apr 30, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants