Compare commits

...

3 Commits

Author SHA1 Message Date
ecfd80f36a fix: typescript transpiler misbehaving 2025-05-22 12:08:41 +03:00
a1a2293af4 fixing gradle (*again*) 2025-05-22 11:35:04 +03:00
6e9250ffd1 minor cleanup 2025-05-22 11:34:29 +03:00
15 changed files with 143 additions and 52 deletions

View File

@@ -1,5 +1,5 @@
plugins {
id("common");
id("j2s-common");
}
java {

View File

@@ -22,26 +22,28 @@ dependencies {
testRuntimeOnly("org.junit.platform:junit-platform-launcher");
}
if (System.getenv("REPO_URL") != null) {
publishing {
repositories {
maven {
name = "Gitea";
url = uri(System.getenv("REPO_URL") ?: "");
publishing {
repositories {
maven {
name = "Gitea";
url = uri(System.getenv("REPO_URL") ?: "");
credentials(HttpHeaderCredentials::class) {
name = "Authorization";
value = "token ${System.getenv("ACCESS_TOKEN")}";
}
credentials(HttpHeaderCredentials::class) {
name = "Authorization";
value = "token ${System.getenv("ACCESS_TOKEN")}";
}
authentication {
create<HttpHeaderAuthentication>("header");
authentication {
create<HttpHeaderAuthentication>("header");
}
}
}
}
publications {
create<MavenPublication>("maven") {
from(components["java"]);
publications {
create<MavenPublication>("maven") {
from(components["java"]);
}
}
}
}

View File

@@ -1,5 +1,5 @@
plugins {
id("common-java");
id("j2s-common-java");
}
description = "A collection of utils and structures for the rest of the project";

View File

@@ -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<Key<Object>, Object> map = new HashMap<>();
private final Set<Key<Object>> 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> T get(Key<T> 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> T get(Key<T> 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> T getWith(Key<T> key, Supplier<T> 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 <T> Environment add(Key<T> key, T val) {
map.put((Key<Object>)key, val);
hidden.remove(key);
return this;
}
/**
* Adds the flag key to the environment
* @return The environment instance
*/
public Environment add(Key<Void> key) {
return add(key, null);
}
/**
* Executes Environment.add for each pair of the map
* @return The environment instance
*/
@SuppressWarnings("all")
public Environment addAll(Map<Key<?>, ?> map, boolean iterableAsMulti) {
public Environment addAll(Map<Key<?>, ?> map) {
map.putAll((Map)map);
hidden.removeAll(map.keySet());
return this;
}
public Environment addAll(Map<Key<?>, ?> 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> T init(Key<T> 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> T initFrom(Key<T> key, Supplier<T> 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();
}

View File

@@ -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());
}

View File

@@ -79,9 +79,9 @@ public class Reading {
if (bytes == null) return null;
else return new String(bytes);
}
public static InputStream resourceToStream(String name) {
return Reading.class.getResourceAsStream("/" + name);
return Reading.class.getResourceAsStream("/" + name.replaceAll("//", "/"));
}
public static String resourceToString(String name) {
return streamToString(resourceToStream(name));

View File

@@ -1,5 +1,5 @@
plugins {
id("common-java");
id("j2s-common-java");
}
description = "A compiler of EcmaScript 5 code to J2S bytecode";

View File

@@ -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

View File

@@ -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 @@
<div class="title-end">СОФИЯ - {{year}}</div>
</div>
<div class="page asm-page">
<div class="page scan-page">
<img src="./img-secret/scan0001.jpg"/>
</div>
<div class="page scan-page">
<img src="./img-secret/scan0003.jpg"/>
</div>
<!-- <div class="page asm-page">
<div class="school-header">
<img class="school-img" src="{{school_img}}"/>
<h4>{{school_name}}</h4>
@@ -561,11 +579,7 @@
</div>
</div>
</div>
</div>
<div class="page">
<div>prazna str</div>
</div>
</div> -->
{{content}}

View File

@@ -1,7 +1,7 @@
import com.github.gradle.node.npm.task.NpmTask;
plugins {
id("common-java");
id("j2s-common-java");
id("com.github.node-gradle.node") version "5.0.0";
}

View File

@@ -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]);

View File

@@ -16,10 +16,10 @@ export default function typescript(next: Compiler): Compiler {
const settings: CompilerOptions = {
target: ScriptTarget.ES5,
module: ModuleKind.Preserve,
allowImportingTsExtensions: true,
verbatimModuleSyntax: true,
verbatimModuleSyntax: false,
strict: false,
skipLibCheck: true,
forceConsistentCasingInFileNames: true,
@@ -52,7 +52,7 @@ export default function typescript(next: Compiler): Compiler {
}
},
getScriptVersion: (filename) => String(versions[filename] || 0),
readFile: () => { throw "no"; },
writeFile: () => { throw "no"; },
}, createDocumentRegistry());
@@ -61,7 +61,7 @@ export default function typescript(next: Compiler): Compiler {
service.getEmitOutput("/lib.d.ts");
});
print("Loaded typescript!");
return (filename, code, prevMap) => {
files["/src.ts"] = ScriptSnapshot.fromString(code);
versions["/src.ts"] ??= 0;

View File

@@ -1,5 +1,5 @@
plugins {
id("common-java");
id("j2s-common-java");
id("com.gradleup.shadow") version "9.0.0-beta4";
}

View File

@@ -1,5 +1,5 @@
plugins {
id("common-java");
id("j2s-common-java");
}
description = "The runtime of J2S, used to execute J2S bytecode";

View File

@@ -15,8 +15,9 @@ public class Arguments {
public final <T extends Value> 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;
}