Skip to content

Commit

Permalink
Added partial Runtime
Browse files Browse the repository at this point in the history
  • Loading branch information
jfd committed Dec 1, 2010
1 parent 6b7f2d4 commit 85f1e3b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 7 deletions.
8 changes: 6 additions & 2 deletions examples/example.conf
@@ -1,10 +1,14 @@
zone = 0x000001

proxy = "test"
name = "Johan"
if (false) {
name = "Johan"
}
end

server
server
path = "./test___a.js"

location
allow = "hej"
deny = "./test.js"
Expand Down
3 changes: 2 additions & 1 deletion examples/example.js
Expand Up @@ -6,11 +6,12 @@ var context = createContext({

// server_name: String

server_name: {type: String, defaultValue: "test"},
server_name: {type: String, value: "test"},

zone: Number,

server: { section: {
path: "path",
location: { section: {
allow: String,
deny: ["path"]
Expand Down
17 changes: 13 additions & 4 deletions lib/conf.js
Expand Up @@ -66,6 +66,7 @@ function ConfigScript(path, filename) {

this.code = readFileSync(resolvedpath, "utf8");
this.filename = basename(path);
this.workdir = dirname(resolvedpath);
}

/**
Expand All @@ -76,17 +77,22 @@ function ConfigScript(path, filename) {
ConfigScript.prototype.runInContext = function(context) {
var sandbox = { __props : {} };
var proplists = [context._fieldProps, context._sectionProps];
var runtime;
var propfn;
var script;
var scope;

if (!context || !context instanceof ConfigContext) {
throw new Error("Expected a ConfigContext as context");
}

runtime = new Runtime(context, this.workdir, false);

proplists.forEach(function(proplist) {
for (var name in proplist) {
propfn = proplist[name].bind(runtime);
Object.defineProperty(sandbox.__props, name, {
get: proplist[name], set: proplist[name]
get: propfn, set: propfn
});
}
});
Expand All @@ -100,15 +106,18 @@ ConfigScript.prototype.runInContext = function(context) {

pushScopeStack(context, context._scope);

// scope.workdir = dirname(resolvedpath);

script.runInNewContext(sandbox);

while (popScopeStack(context));

return context._scope.result;
}

function Runtime(context, workdir, strict) {
this.context = context;
this.workdir = workdir;
this.strict = strict;
}

/**
* ## ConfigContext
Expand Down Expand Up @@ -321,7 +330,7 @@ function createFieldProp(self, name) {
} else if (args.length > 1 || (name in scope.result)) {
throw new Error("conf[" + name + "]: Expected one value only");
} else {
scope.result[name] = validateValue(name, args[0], struct, scope.root);
scope.result[name] = validateValue(name, args[0], struct, this);
}

return scope.result[name];
Expand Down

0 comments on commit 85f1e3b

Please sign in to comment.