int handle_vars(struct JsonlangVm *vm, PyObject *map, int code, int tla) { if (map == NULL) return 1; PyObject *key, *val; Py_ssize_t pos = 0; while (PyDict_Next(map, &pos, &key, &val)) { const char *key_ = PyString_AsString(key); if (key_ == NULL) { jsonlang_destroy(vm); return 0; } const char *val_ = PyString_AsString(val); if (val_ == NULL) { jsonlang_destroy(vm); return 0; } if (!tla && !code) { jsonlang_ext_var(vm, key_, val_); } else if (!tla && code) { jsonlang_ext_code(vm, key_, val_); } else if (tla && !code) { jsonlang_tla_var(vm, key_, val_); } else { jsonlang_tla_code(vm, key_, val_); } } return 1; }
static PyObject* evaluate_snippet(PyObject* self, PyObject* args, PyObject *keywds) { const char *filename, *src; char *out; unsigned max_stack = 500, gc_min_objects = 1000, max_trace = 20; double gc_growth_trigger = 2; int debug_ast = 0, error; PyObject *ext_vars = NULL; struct jsonlangVm *vm; static char *kwlist[] = {"filename", "src", "max_stack", "gc_min_objects", "gc_growth_trigger", "ext_vars", "debug_ast", "max_trace", NULL}; (void) self; if (!PyArg_ParseTupleAndKeywords(args, keywds, "ss|IIdOiI", kwlist, &filename, &src, &max_stack, &gc_min_objects, &gc_growth_trigger, &ext_vars, &debug_ast, &max_trace)) { return NULL; } vm = jsonlang_make(); jsonlang_max_stack(vm, max_stack); jsonlang_gc_min_objects(vm, gc_min_objects); jsonlang_max_trace(vm, max_trace); jsonlang_gc_growth_trigger(vm, gc_growth_trigger); jsonlang_debug_ast(vm, debug_ast); if (ext_vars != NULL) { PyObject *key, *val; Py_ssize_t pos = 0; while (PyDict_Next(ext_vars, &pos, &key, &val)) { const char *key_ = PyString_AsString(key); if (key_ == NULL) { jsonlang_destroy(vm); return NULL; } const char *val_ = PyString_AsString(val); if (val_ == NULL) { jsonlang_destroy(vm); return NULL; } jsonlang_ext_var(vm, key_, val_); } } out = jsonlang_evaluate_snippet(vm, filename, src, &error); if (error) { PyErr_SetString(PyExc_RuntimeError, out); jsonlang_realloc(vm, out, 0); jsonlang_destroy(vm); return NULL; } else { PyObject *ret = PyString_FromString(out); jsonlang_realloc(vm, out, 0); jsonlang_destroy(vm); return ret; } }