Skip to content

Commit

Permalink
feat: implementation new JavaScript converter engine
Browse files Browse the repository at this point in the history
  • Loading branch information
TimeBather committed Jan 3, 2025
1 parent 7048d6a commit 6ed57db
Show file tree
Hide file tree
Showing 41 changed files with 817 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/main/java/kasuga/lib/core/addons/node/AssetReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import kasuga.lib.core.addons.resource.ResourceProvider;
import kasuga.lib.core.javascript.JavascriptContext;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;

import java.io.IOException;
import java.io.InputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kasuga.lib.core.client.frontend.common.event;

import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;

public abstract class DomEvent extends Event{
protected final DomNode<?> currentTarget;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package kasuga.lib.core.client.frontend.common.event;

import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;

public abstract class Event {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
import com.caoccao.javet.annotations.V8Allow;
import com.caoccao.javet.annotations.V8Convert;
import com.caoccao.javet.enums.V8ConversionMode;
import com.caoccao.javet.enums.V8ProxyMode;
import kasuga.lib.core.javascript.CompoundTagWrapper;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import net.minecraft.nbt.CompoundTag;

@V8Convert(mode = V8ConversionMode.AllowOnly)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kasuga.lib.core.client.frontend.common.style;

import com.caoccao.javet.annotations.V8Convert;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import kasuga.lib.core.util.Callback;

import java.util.*;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import kasuga.lib.core.client.frontend.dom.registration.DOMPriorityRegistry;
import kasuga.lib.core.client.frontend.dom.registration.DOMRegistryItemDynamicProxy;
import kasuga.lib.core.javascript.Tickable;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import kasuga.lib.core.javascript.engine.JavascriptValue;
import kasuga.lib.core.util.Callback;
import net.minecraft.resources.ResourceLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import kasuga.lib.core.client.frontend.dom.event.EventEmitter;
import kasuga.lib.core.client.frontend.rendering.RenderContext;
import kasuga.lib.core.javascript.JavascriptContext;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import kasuga.lib.core.javascript.engine.JavascriptValue;

import java.util.ArrayList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import com.mojang.blaze3d.vertex.PoseStack;
import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.frontend.common.layouting.LayoutBox;
import kasuga.lib.core.client.frontend.gui.events.MouseClickEvent;
import kasuga.lib.core.client.frontend.gui.events.MouseReleasedEvent;
import kasuga.lib.core.client.frontend.gui.events.mouse.MouseClickEvent;
import kasuga.lib.core.client.frontend.gui.events.mouse.MouseReleasedEvent;
import kasuga.lib.core.client.frontend.rendering.RenderContext;
import kasuga.lib.core.util.data_type.Vec2i;
import net.minecraft.client.gui.screens.Screen;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package kasuga.lib.core.client.frontend.gui.events;

import kasuga.lib.core.client.frontend.common.event.DomEvent;
import kasuga.lib.core.client.frontend.dom.nodes.DomNode;

public abstract class UIEvent extends DomEvent {
protected UIEvent(DomNode<?> currentTarget, DomNode<?> target, boolean trusted) {
super(currentTarget, target, trusted);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kasuga.lib.core.client.frontend.gui.events;
package kasuga.lib.core.client.frontend.gui.events.mouse;

import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.client.frontend.gui.nodes.GuiDomNode;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package kasuga.lib.core.client.frontend.gui.events;
package kasuga.lib.core.client.frontend.gui.events.mouse;

import kasuga.lib.core.client.frontend.common.event.DomEvent;
import kasuga.lib.core.client.frontend.common.event.Event;
import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.client.frontend.gui.events.UIEvent;
import kasuga.lib.core.client.frontend.gui.nodes.GuiDomNode;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import kasuga.lib.core.util.data_type.Vec2i;

public class MouseEvent extends DomEvent {
public class MouseEvent extends UIEvent {

protected final Vec2i screenPosition;
protected final int button;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kasuga.lib.core.client.frontend.gui.events;
package kasuga.lib.core.client.frontend.gui.events.mouse;

import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.client.frontend.gui.nodes.GuiDomNode;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@

import kasuga.lib.KasugaLib;
import kasuga.lib.core.client.frontend.common.layouting.LayoutBox;
import kasuga.lib.core.client.frontend.common.layouting.LayoutContext;
import kasuga.lib.core.client.frontend.common.layouting.LayoutNode;
import kasuga.lib.core.client.frontend.gui.GuiContext;
import kasuga.lib.core.client.frontend.gui.canvas.CanvasRenderingContext2D;
import kasuga.lib.core.client.frontend.gui.canvas.glfw.CanvasRenderer;
import kasuga.lib.core.client.frontend.rendering.RenderContext;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;

import java.util.Map;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@
import kasuga.lib.core.client.frontend.dom.nodes.DomNode;
import kasuga.lib.core.client.frontend.font.ExtendableProperty;
import kasuga.lib.core.client.frontend.gui.GuiContext;
import kasuga.lib.core.client.frontend.gui.events.MouseEvent;
import kasuga.lib.core.client.frontend.gui.events.mouse.MouseEvent;
import kasuga.lib.core.client.frontend.gui.layout.EdgeSize2D;
import kasuga.lib.core.client.frontend.rendering.BackgroundRenderer;
import kasuga.lib.core.client.frontend.rendering.RenderContext;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import net.minecraft.client.Minecraft;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraftforge.common.util.Lazy;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package kasuga.lib.core.javascript;

import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import net.minecraft.nbt.CompoundTag;
import net.minecraft.nbt.ListTag;
import net.minecraft.nbt.Tag;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;

public class CompoundTagWrapper {
CompoundTag tag;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package kasuga.lib.core.javascript.engine;
package kasuga.lib.core.javascript.engine.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package kasuga.lib.core.javascript.engine.annotations;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

public class Optimization {

@Target({ElementType.CONSTRUCTOR, ElementType.FIELD, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface DisableRuntimeAccessibilityCheck{}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,11 @@
import com.caoccao.javet.interop.callback.IJavetDirectCallable;
import com.caoccao.javet.interop.callback.JavetCallbackContext;
import com.caoccao.javet.interop.callback.JavetCallbackType;
import com.caoccao.javet.interop.converters.JavetProxyConverter;
import com.caoccao.javet.values.V8Value;
import com.caoccao.javet.values.reference.*;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;
import kasuga.lib.core.javascript.engine.JavascriptValue;
import kasuga.lib.core.javascript.engine.javet.JavetJavascriptValue;
import kasuga.lib.core.javascript.engine.javet.JavetKasugaConverter;
import kasuga.lib.core.javascript.engine.javet.JavetValue;
import kasuga.lib.core.util.data_type.Pair;

import java.lang.reflect.*;
Expand Down Expand Up @@ -272,6 +270,8 @@ public V8Value invoke(Object object, ArrayList<Method> alternativeMethods, V8Val
for (Method sameNameMethod : alternativeMethods) {
if(!sameNameMethod.canAccess(object))
continue;
if(sameNameMethod.getParameterCount() != argsList.size())
continue;
Pair<Boolean, V8Value> callResult = tryInvoke(sameNameMethod, argsList, converted);
if(callResult.getFirst()){
if(callResult.getSecond() instanceof V8ValueReference reference){
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import kasuga.lib.core.addons.node.NodePackage;
import kasuga.lib.core.javascript.JavascriptContext;
import kasuga.lib.core.javascript.engine.*;
import kasuga.lib.core.javascript.engine.javet.converter.FastJavetClassConverter;

import java.io.*;
import java.nio.charset.StandardCharsets;
Expand All @@ -25,7 +26,7 @@ public class JavetContext implements JavascriptEngineContext {
JavetStandardConsoleInterceptor consoleInterceptor = new JavetStandardConsoleInterceptor(runtime);
consoleInterceptor.register(runtime.getGlobalObject());
moduleAPI = new JavetModuleAPI(runtime,this, context.getModuleLoader());
runtime.setConverter(new JavetKasugaConverter(runtime));
runtime.setConverter(new FastJavetClassConverter(runtime));
runtime.setPromiseRejectCallback((event, promise, value)->{
if(event.getCode() == 0){
System.err.println("Error" + event.getName());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,29 +1,15 @@
package kasuga.lib.core.javascript.engine.javet;

import com.caoccao.javet.annotations.V8Convert;
import com.caoccao.javet.annotations.V8Function;
import com.caoccao.javet.exceptions.JavetException;
import com.caoccao.javet.interfaces.IJavetEntityFunction;
import com.caoccao.javet.interop.V8Runtime;
import com.caoccao.javet.interop.callback.JavetCallbackContext;
import com.caoccao.javet.interop.converters.JavetObjectConverter;
import com.caoccao.javet.interop.converters.JavetProxyConverter;
import com.caoccao.javet.utils.receivers.JavetCallbackReceiver;
import com.caoccao.javet.values.V8Value;
import com.caoccao.javet.values.reference.V8ValueFunction;
import com.caoccao.javet.values.reference.V8ValueObject;
import com.caoccao.javet.values.reference.V8ValueReference;
import com.caoccao.javet.values.reference.V8ValueSymbol;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.JavascriptValue;
import kasuga.lib.core.util.WeakCache;
import kasuga.lib.core.util.data_type.Pair;

import java.lang.ref.WeakReference;
import java.lang.reflect.*;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.atomic.AtomicReference;

/*
THIS FILE WAS MODIFIED FROM https://www.caoccao.com/Javet/reference/converters/custom_converter.html
Expand All @@ -36,7 +22,7 @@ public class JavetKasugaConverter extends JavetObjectConverter {
private final V8ValueSymbol SYMBOL_NATIVE_OBJECT;
HashMap<Integer, WeakReference<Object>> cachedObjects = new HashMap<>();

JavetKasugaConverter(V8Runtime runtime){
public JavetKasugaConverter(V8Runtime runtime){
this.runtime = runtime;
try{
SYMBOL_NATIVE_OBJECT = runtime.createV8ValueSymbol("NATIVE OBJECT");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package kasuga.lib.core.javascript.engine.javet;

import com.caoccao.javet.values.V8Value;
import kasuga.lib.core.javascript.engine.HostAccess;
import kasuga.lib.core.javascript.engine.annotations.HostAccess;

public interface RequireFunction {
@HostAccess.Export
Expand Down
Loading

0 comments on commit 6ed57db

Please sign in to comment.