Skip to content

Commit 6431ed9

Browse files
committed
executor test code uses stack walker instead of stack trace
deprecated API is deleted from ScriptBasic
1 parent f277b5a commit 6431ed9

6 files changed

Lines changed: 50 additions & 101 deletions

File tree

pom.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,6 @@
155155
</execution>
156156
</executions>
157157
</plugin>
158-
<!--
159158
<plugin>
160159
<groupId>org.apache.maven.plugins</groupId>
161160
<artifactId>maven-gpg-plugin</artifactId>
@@ -170,7 +169,6 @@
170169
</execution>
171170
</executions>
172171
</plugin>
173-
-->
174172
</plugins>
175173
</build>
176174
<distributionManagement>

site/advanced/nativeapi.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ arguments and getting return values.
331331
engine.eval("sub applePie\nglobal a\na = \"hello world\"\nEndSub");
332332
String a = (String) engine.getVariable("a");
333333
assertNull(a);
334-
engine.call("applePie", (Object[]) null);
334+
engine.getSubroutine("applePie").call( (Object[]) null);
335335
a = (String) engine.getVariable("a");
336336
assertEquals("hello world", a);
337337
```
@@ -345,7 +345,7 @@ arguments and getting return values.
345345
String a = (String) engine.getVariable("a");
346346
assertNull(a);
347347
@SuppressWarnings("deprecation")
348-
Long ret = (Long) engine.call("applePie", "hello world");
348+
Long ret = (Long) engine.getSubroutine("applePie").call( "hello world");
349349
a = (String) engine.getVariable("a");
350350
assertEquals("hello world", a);
351351
assertEquals((Long) 6L, ret);

src/main/java/com/scriptbasic/Engine.java

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -217,34 +217,21 @@ public Iterable<String> getVariablesIterator() {
217217
return ctx.interpreter.getVariables().getGlobalMap().getVariableNameSet();
218218
}
219219

220-
@Override
221-
public Object call(final String subroutineName, final Object... args)
222-
throws ScriptBasicException {
223-
try {
224-
return ctx.interpreter.call(subroutineName, args);
225-
} catch (final ExecutionException e) {
226-
throw new ScriptBasicException(e);
227-
}
228-
}
229220

230-
@Override
231-
public Iterable<String> getSubroutineNames() {
232-
return ctx.interpreter.getProgram().getNamedCommandNames();
233-
}
234221

235-
private void SubroutineDoesNotExistWTF(final Exception e) {
222+
private void SNAFU_SubroutineDoesNotExist(final Exception e) {
236223
throw new BasicInterpreterInternalError(
237224
"An already located subroutine does not exist", e);
238225
}
239226

240227
@Override
241228
public Iterable<Subroutine> getSubroutines() {
242229
if (theMapHasToBeFilled) {
243-
for (final String s : getSubroutineNames()) {
230+
for (final String s : ctx.interpreter.getProgram().getNamedCommandNames()) {
244231
try {
245232
getSubroutine(s);
246233
} catch (final ScriptBasicException e) {
247-
SubroutineDoesNotExistWTF(e);
234+
SNAFU_SubroutineDoesNotExist(e);
248235
}
249236
}
250237
theMapHasToBeFilled = false;
@@ -262,8 +249,7 @@ private CommandSub getCommandSub(final String subroutineName)
262249
return commandSub;
263250
}
264251

265-
@Override
266-
public int getNumberOfArguments(final String subroutineName)
252+
private int getNumberOfArguments(final String subroutineName)
267253
throws ScriptBasicException {
268254
final CommandSub commandSub = getCommandSub(subroutineName);
269255
final int size;
@@ -305,7 +291,7 @@ public int getNumberOfArguments() {
305291
try {
306292
return Engine.this.getNumberOfArguments(name);
307293
} catch (final ScriptBasicException e) {
308-
SubroutineDoesNotExistWTF(e);
294+
SNAFU_SubroutineDoesNotExist(e);
309295
return 0;// will not get here
310296
}
311297
}
@@ -317,7 +303,11 @@ public String getName() {
317303

318304
@Override
319305
public Object call(final Object... args) throws ScriptBasicException {
320-
return Engine.this.call(name, args);
306+
try {
307+
return ctx.interpreter.call(name, args);
308+
} catch (final ExecutionException e) {
309+
throw new ScriptBasicException(e);
310+
}
321311
}
322312

323313
@Override

src/main/java/com/scriptbasic/api/ScriptBasic.java

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -262,50 +262,6 @@ void eval(String sourceName, SourceProvider provider)
262262
*/
263263
Iterable<Subroutine> getSubroutines();
264264

265-
/**
266-
* Call the named subroutine with the arguments.
267-
* <p>
268-
* It is recommended to call subroutines via a
269-
* {@link Subroutine} object.
270-
*
271-
* @param subroutineName the name of the subroutine to be called.
272-
* @param args the arguments to be passed to the subroutine. Note that there
273-
* has to be that many arguments passed as many arguments are
274-
* needed by the subroutine. If there are less number of actual
275-
* arguments the rest of the arguments will be undefined. If you
276-
* pass more actual arguments than the subroutine expects you
277-
* will get an exception.
278-
* @return the value returned by the subroutine. Note that this value is
279-
* already converted to Java object and not a raw ScriptBasic
280-
* {@code RightValue} but rather a {@code Long}, {@code Double},
281-
* {@code String} or similar.
282-
* @throws ScriptBasicException
283-
* @deprecated
284-
*/
285-
Object call(String subroutineName, Object... args)
286-
throws ScriptBasicException;
287-
288-
/**
289-
* Get the names of all subroutines.
290-
*
291-
* @return the iterator that can be used to iterate through the names of all
292-
* the subroutines defined in the program.
293-
*/
294-
Iterable<String> getSubroutineNames();
295-
296-
/**
297-
* Get the number of expected argument of the named subroutine.
298-
* <p>
299-
* It is recommended to call subroutines via a
300-
* {@link Subroutine} object.
301-
*
302-
* @param subroutineName the name of the subroutine.
303-
* @return the number of arguments the subroutine expects.
304-
* @throws ScriptBasicException
305-
* @deprecated
306-
*/
307-
int getNumberOfArguments(String subroutineName) throws ScriptBasicException;
308-
309265
/**
310266
* Register the static methods of the class as BASIC functions. After the
311267
* registration the methods can be called from BASIC just as if they were

src/test/java/com/scriptbasic/Executor.java

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import java.io.*;
1111
import java.util.Map;
12+
import java.util.Optional;
1213

1314
/**
1415
* Execute a BASIC program available in the resource file. stdin, stdout and
@@ -26,27 +27,33 @@ public void setMap(Map<String, Object> map) {
2627

2728
public void execute(String resourceName) throws AnalysisException,
2829
ExecutionException, ClassNotFoundException {
29-
StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
30-
InputStream is = Class.forName(stackTrace[2].getClassName())
31-
.getResourceAsStream(resourceName);
32-
final Reader r = new InputStreamReader(is);
33-
final Context ctx = ContextBuilder.from(r);
34-
ctx.interpreter.registerFunctions(FileHandlingFunctions.class);
35-
ctx.interpreter.setProgram(ctx.syntaxAnalyzer.analyze());
36-
StringWriter writer = new StringWriter();
37-
ctx.interpreter.setOutput(writer);
38-
StringWriter errorWriter = new StringWriter();
39-
ctx.interpreter.setError(errorWriter);
40-
StringReader inputReader = getSStdin() == null ? null
41-
: new StringReader(getSStdin());
42-
ctx.interpreter.setInput(inputReader);
43-
if (map != null) {
44-
for (String key : map.keySet()) {
45-
ctx.interpreter.setVariable(key, map.get(key));
30+
Optional<StackWalker.StackFrame> frame =
31+
StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE).walk(s -> s.skip(1).findFirst());
32+
if (frame.isPresent()) {
33+
34+
InputStream is = Class.forName(frame.get().getClassName())
35+
.getResourceAsStream(resourceName);
36+
final Reader r = new InputStreamReader(is);
37+
final Context ctx = ContextBuilder.from(r);
38+
ctx.interpreter.registerFunctions(FileHandlingFunctions.class);
39+
ctx.interpreter.setProgram(ctx.syntaxAnalyzer.analyze());
40+
StringWriter writer = new StringWriter();
41+
ctx.interpreter.setOutput(writer);
42+
StringWriter errorWriter = new StringWriter();
43+
ctx.interpreter.setError(errorWriter);
44+
StringReader inputReader = getSStdin() == null ? null
45+
: new StringReader(getSStdin());
46+
ctx.interpreter.setInput(inputReader);
47+
if (map != null) {
48+
for (String key : map.keySet()) {
49+
ctx.interpreter.setVariable(key, map.get(key));
50+
}
4651
}
52+
ctx.interpreter.execute();
53+
setSStdout(writer.toString());
54+
} else {
55+
throw new ClassNotFoundException();
4756
}
48-
ctx.interpreter.execute();
49-
setSStdout(writer.toString());
5057
}
5158

5259
public void assertOutput(String s) {

src/test/java/com/scriptbasic/TestEngine.java

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.io.StringWriter;
2020
import java.util.HashMap;
2121
import java.util.Map;
22+
import java.util.concurrent.atomic.AtomicInteger;
2223

2324
import static org.junit.Assert.assertEquals;
2425
import static org.junit.Assert.assertNull;
@@ -192,7 +193,7 @@ public void testSubroutineCallWOArgumentsWORetvalLocalVarIsLocal()
192193
engine.eval("sub applePie\na = \"hello world\"\nEndSub");
193194
String a = (String) engine.getVariable("a");
194195
assertNull(a);
195-
engine.call("applePie", (Object[]) null);
196+
engine.getSubroutine("applePie").call((Object[]) null);
196197
a = (String) engine.getVariable("a");
197198
assertNull(a);
198199
}
@@ -205,7 +206,7 @@ public void testSubroutineCallWOArgumentsWORetval() throws Exception {
205206
engine.eval("sub applePie\nglobal a\na = \"hello world\"\nEndSub");
206207
String a = (String) engine.getVariable("a");
207208
assertNull(a);
208-
engine.call("applePie", (Object[]) null);
209+
engine.getSubroutine("applePie").call( (Object[]) null);
209210
a = (String) engine.getVariable("a");
210211
assertEquals("hello world", a);
211212
// END SNIPPET: subroutineCallWOArgumentsWORetval
@@ -219,7 +220,7 @@ public void testSubroutineCallWArgumentsWORetval() throws Exception {
219220
engine.eval("sub applePie(b)\nglobal a\na = b\nEndSub");
220221
String a = (String) engine.getVariable("a");
221222
assertNull(a);
222-
engine.call("applePie", "hello world");
223+
engine.getSubroutine("applePie").call( "hello world");
223224
a = (String) engine.getVariable("a");
224225
assertEquals("hello world", a);
225226
// END SNIPPET: subroutineCallWArgumentsWORetval
@@ -258,7 +259,7 @@ public void testSubroutineCallWArgumentsWORetvalOO() throws Exception {
258259
public void testSubroutineCallWArgumentsWRetval1() throws Exception {
259260
ScriptBasic engine = ScriptBasic.getEngine();
260261
engine.eval("sub applePie(b)\nglobal a\na = b\nreturn 6\nEndSub");
261-
engine.call("applePie", "hello world", "mama");
262+
engine.getSubroutine("applePie").call( "hello world", "mama");
262263
}
263264

264265
@SuppressWarnings("deprecation")
@@ -267,7 +268,7 @@ public void testSubroutineCallWArgumentsWRetval2() throws Exception {
267268
ScriptBasic engine = ScriptBasic.getEngine();
268269
engine.eval("sub applePie(b,c)\nglobal a\na = c\nreturn 6\nEndSub");
269270
String a = (String) engine.getVariable("a");
270-
engine.call("applePie", "hello world");
271+
engine.getSubroutine("applePie").call( "hello world");
271272
a = (String) engine.getVariable("a");
272273
assertNull(a);
273274
}
@@ -280,7 +281,7 @@ public void testSubroutineCallWArgumentsWRetval() throws Exception {
280281
String a = (String) engine.getVariable("a");
281282
assertNull(a);
282283
@SuppressWarnings("deprecation")
283-
Long ret = (Long) engine.call("applePie", "hello world");
284+
Long ret = (Long) engine.getSubroutine("applePie").call( "hello world");
284285
a = (String) engine.getVariable("a");
285286
assertEquals("hello world", a);
286287
assertEquals((Long) 6L, ret);
@@ -308,14 +309,11 @@ public void testSubroutineList() throws Exception {
308309
// START SNIPPET: subroutineList
309310
ScriptBasic engine = ScriptBasic.getEngine();
310311
engine.eval("sub applePie(b)\nEndSub\nsub anotherSubroutine\nEndSub\n");
311-
int i = 0;
312-
for (@SuppressWarnings("unused")
313-
String subName : engine.getSubroutineNames()) {
314-
i++;
315-
}
316-
assertEquals(2, i);
317-
assertEquals(1, engine.getNumberOfArguments("applePie"));
318-
assertEquals(0, engine.getNumberOfArguments("anotherSubroutine"));
312+
final AtomicInteger i = new AtomicInteger(0);
313+
engine.getSubroutines().forEach( (s) ->i.incrementAndGet());
314+
assertEquals(2, i.get());
315+
assertEquals(1, engine.getSubroutine("applePie").getNumberOfArguments());
316+
assertEquals(0, engine.getSubroutine("anotherSubroutine").getNumberOfArguments());
319317
// END SNIPPET: subroutineList
320318
}
321319

0 commit comments

Comments
 (0)