Skip to content

Commit

Permalink
Adding script.core.AppImpl
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed Mar 2, 2024
1 parent 2dc5bd8 commit f41c11c
Show file tree
Hide file tree
Showing 11 changed files with 179 additions and 31 deletions.
14 changes: 10 additions & 4 deletions src/main/java/org/luwrain/core/Core.java
Original file line number Diff line number Diff line change
Expand Up @@ -168,14 +168,20 @@ private void init()
uiSettings = Settings.createUserInterface(registry);
}

String loadScript(ScriptFile scriptFile) throws ExtensionException
String loadScript(ScriptSource script) throws ExtensionException
{
notNull(scriptFile, "scriptFile");
notNull(script, "script");
mainCoreThreadOnly();
final var ext = new org.luwrain.script.core.ScriptExtension(scriptFile.toString());
final var ext = new org.luwrain.script.core.ScriptExtension(script.toString()){
@Override public void launchApp(Application app)
{
notNull(app, "app");
Core.this.launchApp(app);
}
};
ext.init(interfaces.requestNew(ext));
try {
ext.getScriptCore().load(scriptFile);
ext.getScriptCore().load(script);
}
catch(Throwable e)
{
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/org/luwrain/core/ExtensionException.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
Expand All @@ -14,8 +14,6 @@
General Public License for more details.
*/

//LWR_API 1.0

package org.luwrain.core;

public final class ExtensionException extends Exception
Expand Down
1 change: 1 addition & 0 deletions src/main/java/org/luwrain/core/Luwrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ public enum JobFlags { TRACKING };
MediaResourcePlayer[] getMediaResourcePlayers();
org.luwrain.player.Player getPlayer();
Registry getRegistry();
String loadScript(ScriptSource scriptSource) throws ExtensionException;
int getScreenWidth();
int getScreenHeight();

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/luwrain/core/LuwrainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,12 @@ final class LuwrainImpl implements Luwrain
return res.toArray(new ScriptFile[res.size()]);
}

@Override public String loadScript(ScriptSource scriptSource) throws ExtensionException
{
notNull(scriptSource, "scriptSource");
return core.loadScript(scriptSource);
}

@Override public String escapeString(String style, String text)
{
notNull(style, "style");
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/luwrain/core/ScriptFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import java.io.*;

public final class ScriptFile
public final class ScriptFile extends ScriptSource
{
private final String component, file, dataDir;

Expand Down
62 changes: 62 additions & 0 deletions src/main/java/org/luwrain/script/core/AppImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
LUWRAIN is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
LUWRAIN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/

package org.luwrain.script.core;

import java.io.*;
import java.util.*;
import java.util.concurrent.*;

import org.graalvm.polyglot.*;

import org.luwrain.core.*;

import static org.luwrain.core.NullCheck.*;

final class AppImpl implements Application
{
final Module module;
final Value construct;
private Value instance = null;

AppImpl(Module module, Value construct)
{
notNull(module, "module");
notNull(construct, "construct");
this.module = module;
this.construct = construct;
}

@Override public void onAppClose()
{
}

@Override public String getAppName()
{
return "";
}

@Override public AreaLayout getAreaLayout()
{
return null;
}

@Override public InitResult onLaunchApp(Luwrain luwrain)
{
this.instance = module.execNewInstance(construct, new Object[0]);
return null;
}
}
24 changes: 24 additions & 0 deletions src/main/java/org/luwrain/script/core/InternalCoreFuncs.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
Copyright 2012-2022 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
LUWRAIN is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
LUWRAIN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/

package org.luwrain.script.core;

import org.luwrain.core.*;

interface InternalCoreFuncs
{
void launchApp(Application app);
}
40 changes: 28 additions & 12 deletions src/main/java/org/luwrain/script/core/LuwrainObj.java
Original file line number Diff line number Diff line change
Expand Up @@ -225,23 +225,39 @@ private Object isSpaceImpl(Value[] values)
}

@HostAccess.Export public final ProxyExecutable launchApp = this::launchAppImpl;
private Object launchAppImpl(Value[] values)
private Object launchAppImpl(Value[] values)
{
if (notNullAndLen(values, 2))
{
if (!values[0].isString() || !values[1].hasArrayElements())
return false;
final String[] args = asStringArray(values[1]);
if (args == null)
return false;
luwrain.launchApp(values[0].asString(), args);
return true;
}
if (!values[1].hasArrayElements())
return false;
final String[] args = asStringArray(values[1]);
if (args == null)
return false;
if (values[0].isString())
{
luwrain.launchApp(values[0].asString(), args);
return true;
}
if (!values[0].canExecute())
throw new IllegalArgumentException("The first argument to Luwrian.launchApp() must be a string or an executable object");
if (module.internalCoreFuncs == null)
throw new IllegalArgumentException("This script core can launch apps by their names only");
module.internalCoreFuncs.launchApp(new AppImpl(module, values[0]));
return true;
}
if (!notNullAndLen(values, 1))
return false;
if (!values[0].isString())
return false;
luwrain.launchApp(values[0].asString());
if (values[0].isString())
{
luwrain.launchApp(values[0].asString());
return true;
}
if (!values[0].canExecute())
throw new IllegalArgumentException("The first argument to Luwrian.launchApp() must be a string or an executable object");
if (module.internalCoreFuncs == null)
throw new IllegalArgumentException("This script core can launch apps by their names only");
module.internalCoreFuncs.launchApp(new AppImpl(module, values[0]));
return true;
}

Expand Down
19 changes: 17 additions & 2 deletions src/main/java/org/luwrain/script/core/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ public final class Module implements AutoCloseable
final Context context;
final Object syncObj = new Object();
private final Luwrain luwrain;
final InternalCoreFuncs internalCoreFuncs;
final LuwrainObj luwrainObj;

public Module(Luwrain luwrain, Bindings bindings)
public Module(Luwrain luwrain, InternalCoreFuncs internalCoreFuncs, Bindings bindings)
{
notNull(luwrain, "luwrain");
this.luwrain = luwrain;
this.internalCoreFuncs = internalCoreFuncs;
this.luwrainObj = new LuwrainObj(luwrain, syncObj, this);
final Engine engine = Engine.newBuilder()
.option("engine.WarnInterpreterOnly", "false")
Expand All @@ -47,9 +49,14 @@ public Module(Luwrain luwrain, Bindings bindings)
bindings.onBindings(context.getBindings("js"), luwrainObj.syncObj);
}

public Module(Luwrain luwrain, Bindings bindings)
{
this(luwrain, null, bindings);
}

public Module(Luwrain luwrain )
{
this(luwrain, null);
this(luwrain, null, null);
}

public Object eval(String exp)
Expand All @@ -66,6 +73,14 @@ public void execFuncValue(Value func)
}
}

public Value execNewInstance(Value construct, Object[] args)
{
synchronized(syncObj){
return construct.newInstance(args);
}
}


@Override public void close()
{
synchronized(syncObj) {
Expand Down
30 changes: 23 additions & 7 deletions src/main/java/org/luwrain/script/core/ScriptCore.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,25 @@ public final class ScriptCore implements HookContainer, AutoCloseable

private final Bindings bindings;
private final Luwrain luwrain;
private final InternalCoreFuncs internalCoreFuncs;
private final List<Module> modules = new ArrayList<>();

public ScriptCore(Luwrain luwrain, Bindings bindings)
public ScriptCore(Luwrain luwrain, InternalCoreFuncs internalCoreFuncs, Bindings bindings)
{
notNull(luwrain, "luwrain");
this.luwrain = luwrain;
this.internalCoreFuncs = internalCoreFuncs;
this.bindings = bindings;
}

public ScriptCore(Luwrain luwrain, Bindings bindings)
{
this(luwrain, null, bindings);
}

public ScriptCore(Luwrain luwrain)
{
this(luwrain, null);
this(luwrain, null, null);
}

@Override public void close()
Expand All @@ -53,7 +60,7 @@ public ScriptCore(Luwrain luwrain)
m.close();
}

public void load (Reader reader) throws IOException
private void load (Reader reader) throws IOException
{
notNull(reader, "reader");
final String lineSep = System.lineSeparator();
Expand All @@ -78,11 +85,20 @@ public void load (File file) throws IOException
modules.add(m);
}

public void load (ScriptFile scriptFile) throws IOException
public void load (ScriptSource scriptSource) throws IOException
{
notNull(scriptFile, "scriptFile");
Log.debug(LOG_COMPONENT, "loading " + scriptFile.toString());
load(scriptFile.asFile());
notNull(scriptSource, "scriptSource");
Log.debug(LOG_COMPONENT, "loading " + scriptSource.toString());
if (scriptSource instanceof ScriptFile f)
{
load(f.asFile());
return;
}
if (scriptSource instanceof ScriptText t)
{
load(new StringReader(t.getText()));
return;
}
}

@Override public boolean runHooks(String hookName, Luwrain.HookRunner runner)
Expand Down
8 changes: 6 additions & 2 deletions src/main/java/org/luwrain/script/core/ScriptExtension.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import static org.luwrain.core.NullCheck.*;

public final class ScriptExtension implements Extension, org.luwrain.core.HookContainer
public abstract class ScriptExtension implements Extension, org.luwrain.core.HookContainer
{
public final String name;
private ScriptCore scriptCore = null;
Expand All @@ -34,11 +34,15 @@ public ScriptExtension(String name)
this.name = name;
}

public abstract void launchApp(Application app);

@Override public String init(Luwrain luwrain)
{
notNull(luwrain, "luwrain");
this.luwrain = luwrain;
this.scriptCore = new ScriptCore(luwrain);
this.scriptCore = new ScriptCore(luwrain, new InternalCoreFuncs(){
@Override public void launchApp(Application app) { ScriptExtension.this.launchApp(app); }
}, null);
return null;
}

Expand Down

0 comments on commit f41c11c

Please sign in to comment.