diff --git a/common/src/main/java/me/topchetoeu/j2s/common/Environment.java b/common/src/main/java/me/topchetoeu/j2s/common/Environment.java index 3163873..6507c0c 100644 --- a/common/src/main/java/me/topchetoeu/j2s/common/Environment.java +++ b/common/src/main/java/me/topchetoeu/j2s/common/Environment.java @@ -6,55 +6,99 @@ import java.util.Map; import java.util.Set; import java.util.function.Supplier; +/** + * The class, used to keep track of important environment-wide values + * Supports an inheritance mechanism via parents + * In essence a simple hashmap of flag objects to values + */ public class Environment { + /** + * The parent of this environment. Will act as a "fallback" when searching for elements. + * Operations this environment won't write into the parent + */ public final Environment parent; private final Map, Object> map = new HashMap<>(); private final Set> hidden = new HashSet<>(); + /** + * Gets the element, contained in this environment, signified by the given key + * @return The element, or null if not found + */ @SuppressWarnings("unchecked") public T get(Key key) { if (map.containsKey(key)) return (T)map.get(key); else if (!hidden.contains(key) && parent != null) return parent.get(key); else return null; } + /** + * Checks if the environment has the given key + */ public boolean has(Key key) { if (map.containsKey(key)) return true; else if (!hidden.contains(key) && parent != null) return parent.has(key); else return false; } + /** + * Checks if the environment has the given key and if the value behind they key is not null + */ public boolean hasNotNull(Key key) { return get(key) != null; } + /** + * Gets the element, contained in this environment, signified by the given key + * @param defaultVal The value to return if the element is not found + * @return The element, or "defaultVal" if not found + */ public T get(Key key, T defaultVal) { if (has(key)) return get(key); else return defaultVal; } + /** + * Gets the element, contained in this environment, signified by the given key + * @param defaultVal The supplier, from which to return if the element is not found + * @return The element, or the result of "defaultVal" if not found + */ public T getWith(Key key, Supplier defaultVal) { if (has(key)) return get(key); else return defaultVal.get(); } + /** + * Inserts the given value for the given key, replacing any existing value + * If a parent has a value with the same key, it isn't replaced, but instead - shadowed + * @return The inserted element + */ @SuppressWarnings("unchecked") public Environment add(Key key, T val) { map.put((Key)key, val); hidden.remove(key); return this; } + /** + * Adds the flag key to the environment + * @return The environment instance + */ public Environment add(Key key) { return add(key, null); } + /** + * Executes Environment.add for each pair of the map + * @return The environment instance + */ @SuppressWarnings("all") - public Environment addAll(Map, ?> map, boolean iterableAsMulti) { + public Environment addAll(Map, ?> map) { map.putAll((Map)map); hidden.removeAll(map.keySet()); return this; } - public Environment addAll(Map, ?> map) { - return addAll(map, true); - } + /** + * Removes the given key from the environment. + * If a parent has the given key, it is instead just "hidden" by this environment + * @return The environment instance + */ @SuppressWarnings("unchecked") public Environment remove(Key key) { map.remove(key); @@ -62,6 +106,9 @@ public class Environment { return this; } + /** + * If the key exists in the environment, returns it. Otherwise, puts the given value and returns the value + */ public T init(Key key, T val) { if (!has(key)) { this.add(key, val); @@ -69,6 +116,9 @@ public class Environment { } else return get(key); } + /** + * If the key exists in the environment, returns it. Otherwise, puts the given value from the supplier and returns it + */ public T initFrom(Key key, Supplier val) { if (!has(key)) { var res = val.get(); @@ -78,6 +128,10 @@ public class Environment { else return get(key); } + /** + * Creates an environment that is a child of this environment + * @return + */ public Environment child() { return new Environment(this); } @@ -85,15 +139,25 @@ public class Environment { public Environment(Environment parent) { this.parent = parent; } + /** + * Creates an empty environment + */ public Environment() { this.parent = null; } + /** + * If the environment is null, returns an empty environment + * Otherwise, returns the given value + */ public static Environment wrap(Environment env) { if (env == null) return empty(); else return env; } + /** + * Returns a newly-created environment + */ public static Environment empty() { return new Environment(); } diff --git a/common/src/main/java/me/topchetoeu/j2s/common/Filename.java b/common/src/main/java/me/topchetoeu/j2s/common/Filename.java index bb3f0f3..a75900e 100644 --- a/common/src/main/java/me/topchetoeu/j2s/common/Filename.java +++ b/common/src/main/java/me/topchetoeu/j2s/common/Filename.java @@ -2,8 +2,17 @@ package me.topchetoeu.j2s.common; import java.io.File; +/** + * The class that represents all filenames in J2S + */ public class Filename { + /** + * The protocol of the filename (file://, http://, ftp://, etc...) + */ public final String protocol; + /** + * The path to the file (/home/me/test.js, example.org/test.js, etc...) + */ public final String path; @Override public String toString() { @@ -42,11 +51,18 @@ public class Filename { this.path = path; } + /** + * Parses the given string to a filename. + * If a :// is not found, the protocol will default to "file" + */ public static Filename parse(String val) { var i = val.indexOf("://"); if (i >= 0) return new Filename(val.substring(0, i).trim(), val.substring(i + 3).trim()); else return new Filename("file", val.trim()); } + /** + * Will convert the File instance to a filename, with the protocol set to "file" + */ public static Filename fromFile(File file) { return new Filename("file", file.getAbsolutePath()); } diff --git a/doc/text/src/json.lua b/doc/text/src/json.lua index 4a43186..0a707b2 100644 --- a/doc/text/src/json.lua +++ b/doc/text/src/json.lua @@ -102,16 +102,17 @@ local function parse_impl(str, pos, end_delim) local delim_found; if c == "{" then - pos = pos + 1; + pos = str:find("%S", pos + 1) or pos; local key; local obj = {}; c = string.sub(str, pos, pos); if c == "}" then - return obj, pos + return obj, pos; else while true do + pos = skip_delim(str, pos); key, pos = parse_str_val(str, pos, true); if key == nil then error("Expected a string key") end diff --git a/doc/text/template.html b/doc/text/template.html index 0b3a3f7..d8a8715 100644 --- a/doc/text/template.html +++ b/doc/text/template.html @@ -14,6 +14,9 @@ content: counter(page); } } + @page scan-page { + margin: 0; + } h1 { break-before: page; } @@ -196,6 +199,13 @@ break-after: page; } + .scan-page { + display: flex; + flex-direction: column; + justify-content: stretch; + align-items: stretch; + page: scan-page; + } .title-page { display: flex; flex-direction: column; @@ -518,7 +528,15 @@
СОФИЯ - {{year}}
-
+
+ +
+ +
+ +
+ + {{content}} diff --git a/lib/src/main/java/me/topchetoeu/j2s/lib/Compilers.java b/lib/src/main/java/me/topchetoeu/j2s/lib/Compilers.java index 2f8335f..682df03 100644 --- a/lib/src/main/java/me/topchetoeu/j2s/lib/Compilers.java +++ b/lib/src/main/java/me/topchetoeu/j2s/lib/Compilers.java @@ -21,12 +21,12 @@ public class Compilers { try { var res = JavaScript.compile(env, filename, raw, true); var body = res.body(); - + DebugHandler.get(env).onSourceLoad(filename, raw); for (var el : res.all()) { DebugHandler.get(env).onFunctionLoad(el.body(), el.map(mapper)); } - + return new CodeFunction(env, filename.toString(), body, new Value[0][]); } catch (SyntaxException e) { @@ -59,9 +59,6 @@ public class Compilers { public static Compiler transpilerFromSource(Compiler prev, Environment target, Filename compilerName, String compilerSrc) { var env = StdLib.apply(null); - // var handler = new SimpleDebugHandler(); - // env.add(DebugHandler.KEY, handler); - var glob = Value.global(env); var compilerFactory = new FunctionValue[1]; @@ -86,10 +83,6 @@ public class Compilers { var compiled = JavaScript.compile(compilerName, compilerSrc, false); - // for (var el : compiled.all()) { - // handler.onFunctionLoad(el.body(), el.map()); - // } - try { new CodeFunction(env, "intializer", compiled.body(), new Value[0][]).apply(env, Value.UNDEFINED); return wrap(prev, env, target, compilerFactory[0]); diff --git a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/functions/Arguments.java b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/functions/Arguments.java index f14ddb0..fed70de 100644 --- a/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/functions/Arguments.java +++ b/runtime/src/main/java/me/topchetoeu/j2s/runtime/values/functions/Arguments.java @@ -15,8 +15,9 @@ public class Arguments { public final T setTargetProto(T obj) { if (!self.isPrimitive()) { var proto = self.getMember(env, "prototype"); - if (proto instanceof ObjectValue objProto) self.setPrototype(env, objProto); - else if (proto == Value.NULL) self.setPrototype(env, null); + + if (proto instanceof ObjectValue objProto) obj.setPrototype(env, objProto); + else if (proto == Value.NULL) obj.setPrototype(env, null); } return obj; }