From 8b16a71d507fa1798e87d2cb1d4016a86e619583 Mon Sep 17 00:00:00 2001 From: Anton Gomez Alvedro Date: Tue, 13 May 2014 20:09:48 +0200 Subject: [PATCH] Adds assert() for Nasal and fixes io.include internal marking - A simple assert() function is added to the globals namespace. - io.include() marks the target namespace to avoid dependency loops. If the namespace is marked before the script to be included is compiled, a parse error leaves the target namespace marked while the script has not been loaded. This patch fixes this problem. --- Nasal/globals.nas | 10 +++++++++- Nasal/io.nas | 4 +--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Nasal/globals.nas b/Nasal/globals.nas index 4be967a86..273611797 100644 --- a/Nasal/globals.nas +++ b/Nasal/globals.nas @@ -27,6 +27,15 @@ var L2GAL = 1 / GAL2L; # container for local variables, so as not to clutter the global namespace var __ = {}; +## +# Aborts execution if evaluates to false. +# Prints an optional message if present, or just "assertion failed!" +# +var assert = func (condition, message=nil) { + message != nil or (message = "assertion failed!"); + condition or die(message); +} + ## # Returns true if the first object is an instance of the second # (class) object. Example: isa(someObject, props.Node) @@ -180,4 +189,3 @@ settimer(func { if(size(file) > 4 and substr(file, -4) == ".nas") io.load_nasal(path ~ "/" ~ file, substr(file, 0, size(file) - 4)); }, 0); - diff --git a/Nasal/io.nas b/Nasal/io.nas index 179af84aa..b27e2aa36 100644 --- a/Nasal/io.nas +++ b/Nasal/io.nas @@ -47,8 +47,6 @@ var include = func(file) { if (contains(ns, module)) return; - ns[module] = "included"; - var code = call(compile, [readfile(path), path], var err = []); if (size(err)) { if (find("Parse error:", err[0]) < 0) @@ -57,6 +55,7 @@ var include = func(file) { die(sprintf("%s\n in included file: %s", err[0], path)); } + ns[module] = "included"; call(bind(code, ns, fn), [], nil, ns); } @@ -425,4 +424,3 @@ _setlistener("/sim/signals/nasal-dir-initialized", func { } })(); }); -