diff options
-rw-r--r-- | interpreter.py | 25 | ||||
-rw-r--r-- | test cases/common/17 if/meson.build | 16 |
2 files changed, 41 insertions, 0 deletions
diff --git a/interpreter.py b/interpreter.py index 9885932..82fdb74 100644 --- a/interpreter.py +++ b/interpreter.py @@ -910,6 +910,8 @@ class Interpreter(): 'pkgconfig_gen' : self.func_pkgconfig_gen, 'vcs_tag' : self.func_vcs_tag, 'set_variable' : self.func_set_variable, + 'is_variable' : self.func_is_variable, + 'get_variable' : self.func_get_variable, 'import' : self.func_import, 'files' : self.func_files, 'declare_dependency': self.func_declare_dependency, @@ -1008,6 +1010,29 @@ class Interpreter(): value = self.to_native(args[1]) self.set_variable(varname, value) + @noKwargs + def func_get_variable(self, node, args, kwargs): + if len(args)<1 or len(args)>2: + raise InvalidCode('Get_variable takes one or two arguments.') + varname = args[0] + if not isinstance(varname, str): + raise InterpreterException('First argument must be a string.') + try: + return self.variables[varname] + except KeyError: + pass + if len(args) == 2: + return args[1] + raise InterpreterException('Tried to get unknown variable "%s".' % varname) + + @stringArgs + @noKwargs + def func_is_variable(self, node, args, kwargs): + if len(args) != 1: + raise InvalidCode('Is_variable takes two arguments.') + varname = args[0] + return varname in self.variables + @stringArgs @noKwargs def func_import(self, node, args, kwargs): diff --git a/test cases/common/17 if/meson.build b/test cases/common/17 if/meson.build index b77f93a..2103d20 100644 --- a/test cases/common/17 if/meson.build +++ b/test cases/common/17 if/meson.build @@ -12,3 +12,19 @@ if var2 endif test('iftest', exe) + +if not is_variable('var1') + error('Is_variable fail.') +endif + +if is_variable('nonexisting') + error('Is_variable fail 2.') +endif + +if not get_variable('var1', false) + error('Get_variable fail.') +endif + +if get_variable('nonexisting', false) + error('Get_variable fail.') +endif |