-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
BuildTools
committed
Oct 3, 2021
1 parent
5871dd8
commit a565b76
Showing
20 changed files
with
784 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
...lange/src/main/java/com/github/terefang/jmelange/scripted/factory/JRubyScriptFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.github.terefang.jmelange.scripted.factory; | ||
|
||
import com.github.terefang.jmelange.commons.CommonUtil; | ||
import com.github.terefang.jmelange.scripted.AbstractScript; | ||
import com.github.terefang.jmelange.scripted.JMelangeScriptFactory; | ||
import com.github.terefang.jmelange.scripted.impl.JRubyScript; | ||
|
||
import java.util.List; | ||
|
||
public class JRubyScriptFactory implements JMelangeScriptFactory { | ||
@Override | ||
public String getName() { | ||
return "jruby"; | ||
} | ||
|
||
static List<String> SCRIPTNAMES = CommonUtil.toList("jruby", "ruby"); | ||
|
||
static List<String> SCRIPTEXTS = CommonUtil.toList(".jrb", ".rb"); | ||
|
||
@Override | ||
public List<String> getScriptNames() { | ||
return SCRIPTNAMES; | ||
} | ||
|
||
@Override | ||
public List<String> getScriptExts() { | ||
return SCRIPTEXTS; | ||
} | ||
|
||
@Override | ||
public AbstractScript createScript() { | ||
JRubyScript _scp = JRubyScript.create(); | ||
return _scp; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
scripted-jmelange/src/main/java/com/github/terefang/jmelange/scripted/impl/JRubyScript.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
package com.github.terefang.jmelange.scripted.impl; | ||
|
||
import com.github.terefang.jmelange.scripted.AbstractScript; | ||
import lombok.SneakyThrows; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.jruby.CompatVersion; | ||
import org.jruby.RubyInstanceConfig; | ||
import org.jruby.embed.EmbedEvalUnit; | ||
import org.jruby.embed.LocalContextScope; | ||
import org.jruby.embed.LocalVariableBehavior; | ||
import org.jruby.embed.ScriptingContainer; | ||
import org.jruby.embed.internal.BiVariableMap; | ||
|
||
import java.io.PrintStream; | ||
import java.io.Reader; | ||
import java.util.Map; | ||
|
||
@Slf4j | ||
public class JRubyScript extends AbstractScript | ||
{ | ||
public static JRubyScript create() { return new JRubyScript(); } | ||
private ScriptingContainer sinstance; | ||
private EmbedEvalUnit scriptCode; | ||
|
||
@Override | ||
@SneakyThrows | ||
public boolean init(Reader _script) | ||
{ | ||
try | ||
{ | ||
this.sinstance = new ScriptingContainer(LocalContextScope.THREADSAFE, LocalVariableBehavior.PERSISTENT); | ||
this.sinstance.setCompileMode(RubyInstanceConfig.CompileMode.FORCE); | ||
this.sinstance.setCompatVersion(CompatVersion.RUBY2_1); | ||
this.scriptCode = this.sinstance.parse(_script, this.getScriptFile().getName(), 0); | ||
} | ||
catch (Exception xe) | ||
{ | ||
log.warn("error in file", xe); | ||
} | ||
finally | ||
{ | ||
return (this.scriptCode != null); | ||
} | ||
} | ||
|
||
@Override | ||
public Object executeObject(boolean _scopeOrBindings) | ||
{ | ||
try | ||
{ | ||
|
||
synchronized (this.sinstance) | ||
{ | ||
this.sinstance.clear(); | ||
|
||
if(this.getArgs()!=null) this.sinstance.setArgv(this.getArgs().toArray(new String[this.getArgs().size()])); | ||
|
||
if(this.getInputStream()!=null) this.sinstance.setInput(this.getInputStream()); | ||
if(this.getOutputStream()!=null) this.sinstance.setOutput(new PrintStream(this.getOutputStream())); | ||
|
||
for (Map.Entry<String, Object> e : this.assembleContext().entrySet()) { | ||
this.sinstance.put("$"+e.getKey(), e.getValue()); | ||
} | ||
|
||
BiVariableMap _vars = this.sinstance.getVarMap(); | ||
|
||
Object _ret = this.scriptCode.run(); | ||
|
||
return _ret; | ||
} | ||
} | ||
catch (Exception xe) | ||
{ | ||
log.warn("error in file", xe); | ||
return null; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.