From 9d80229653d060540c22a3f169822b8e2df6c8b0 Mon Sep 17 00:00:00 2001 From: Pavel Shevaev Date: Tue, 18 Jul 2023 15:22:57 +0300 Subject: [PATCH] Prototyping native modules support --- src/bind.inc.php | 37 +++++++++++++++++++++++++++++++++++++ tpl/codegen_autobind.twig | 9 +++++++++ 2 files changed, 46 insertions(+) diff --git a/src/bind.inc.php b/src/bind.inc.php index ccf84fd..3bff4e8 100644 --- a/src/bind.inc.php +++ b/src/bind.inc.php @@ -242,6 +242,31 @@ function add_twig_support(\Twig\Environment $twig) return $sliced; } )); + $twig->addFunction(new \Twig\TwigFunction('get_modules', + function(\mtgMetaInfo $m) + { + $modules = array(); + foreach($m->getUnits() as $u) + { + $tmp = $u->object->getToken('bhl_bind'); + if(!$tmp || !is_string($tmp)) + continue; + $m = module_path($tmp); + if($m) + $modules[$m] = module_name($m); + } + return $modules; + } + )); + $twig->addFunction(new \Twig\TwigFunction('module_scope_or', + function($o, $default_val) + { + $m = $o->getToken("bhl_bind"); + if(!$m || !is_string($m)) + return $default_val; + return "Module".module_name($m).".ns"; + } + )); } function Warn($msg) @@ -289,6 +314,18 @@ function ns_prefix($name) return $prefix; } +function module_path($path) +{ + return trim($path, '"'); +} + +function module_name($name) +{ + $name = str_replace('/', '_', $name); + $name = str_replace('.', '_', $name); + return $name; +} + function norm_name($name) { return str_replace('.', '__', $name); diff --git a/tpl/codegen_autobind.twig b/tpl/codegen_autobind.twig index 9267023..fc35f38 100644 --- a/tpl/codegen_autobind.twig +++ b/tpl/codegen_autobind.twig @@ -28,6 +28,10 @@ static public class Types_{{u.object.name|norm_name}} { static public class {{register_class}} { +{%- for mk,mv in get_modules(meta) ~%} + static public Module Module{{mv}}; +{%- endfor ~%} + public static void Register(Types types) { RegisterBegin(types); @@ -36,6 +40,11 @@ public static void Register(Types types) public static void RegisterBegin(Types types) { +{%- for mk,mv in get_modules(meta) ~%} + Module{{mv}} = new Module(types, "{{mk}}"); + types.RegisterModule(Module{{mv}}); +{%- endfor ~%} + {{ macro.call_reg_units(slice_units(meta.units, 20)) }} }