Skip to content

Commit

Permalink
Added Java accesors to variables the JS scope (closes #8)
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Mar 3, 2017
1 parent a4fe6a5 commit 23b667b
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 16 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ a link to this page somewhere in the documentation/system about section.

## Change log for the BPjs library.

### 2017-03-02

* :sparkles: `bp.random` added.
* :arrows_counterclockwise: Documentation updates
* :sparkles: Added java accessors for putting and getting variables in the JS program
* :arrows_counterclockwise: `fat.jar` is now `uber.jar`.

### 2017-02-03

* :sparkles: the standard `.jar` file now contains only BPjs, and no dependencies. Fat jar (the jar that includes dependencies) is available via the releases tab.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,17 @@
import static il.ac.bgu.cs.bp.bpjs.eventsets.EventSets.all;
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsCodeEvaluationException;
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsException;
import il.ac.bgu.cs.bp.bpjs.exceptions.BPjsRuntimeException;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import static java.nio.file.Files.readAllBytes;
import java.util.logging.Level;
import java.util.logging.Logger;
import static java.util.stream.Collectors.toList;
import org.mozilla.javascript.Context;
import org.mozilla.javascript.ContextFactory;
import org.mozilla.javascript.ImporterTopLevel;
import org.mozilla.javascript.Scriptable;
import static java.util.stream.Collectors.toSet;
import static java.nio.file.Paths.get;
import static java.util.Collections.reverseOrder;
import java.util.concurrent.atomic.AtomicInteger;
import org.mozilla.javascript.ContinuationPending;
Expand Down Expand Up @@ -556,7 +545,33 @@ public boolean isDaemonMode() {
public Scriptable getGlobalScope() {
return programScope;
}


/**
* Adds an object to the program's global scope. JS code can reference the
* added object by {@code name}.
* @param name The name under which {@code object} will be available to the JS code.
* @param obj The object to be added to the program's scope.
*/
public void putInGlobalScope( String name, Object obj ) {
getGlobalScope().put(name, programScope, Context.javaToJS(obj, programScope));
}

/**
* Gets the object pointer by the passed name in the global scope.
* @param <T> Class of the returned object.
* @param name Name of the object in the JS heap.
* @param clazz Class of the returned object
* @return The object pointer by the passed name in the JS heap, converted to
* the passed class.
*/
public <T> Optional<T> getFromGlobalScope( String name, Class<T> clazz ) {
if ( getGlobalScope().has(name, programScope) ) {
return Optional.of( (T)Context.jsToJava(getGlobalScope().get(name, getGlobalScope()), clazz) );
} else {
return Optional.empty();
}
}

/**
* Returns the snapshots of all current BThreads. This method will only yield
* meaningful results when the program is at BSync state.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* The MIT License
*
* Copyright 2017 michael.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import org.junit.Test;

/**
*
* @author michael
*/
public class BProgramTest {

@Test
public void testGlobalScopeAccessors() throws InterruptedException {
BProgram sut = new SingleResourceBProgram("RandomProxy.js");

sut.start();

assertEquals( 1000.0, sut.getFromGlobalScope("TOTAL_COUNT", Double.class).get(), 3 );
assertFalse( sut.getFromGlobalScope("does-not-exist", Double.class).isPresent() );
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.BProgram;
import il.ac.bgu.cs.bp.bpjs.bprogram.runtimeengine.SingleResourceBProgram;
import org.junit.Test;
import org.mozilla.javascript.Context;
import static org.junit.Assert.assertEquals;

/**
Expand All @@ -41,13 +40,13 @@ public void randomProxyText() throws InterruptedException {
BProgram sut = new SingleResourceBProgram("RandomProxy.js");

sut.start();
Double boolCount = (Double)Context.jsToJava(sut.getGlobalScope().get("boolCount", sut.getGlobalScope()), Double.class);
Double boolCount = sut.getFromGlobalScope("boolCount", Double.class).get();
assertEquals(500.0, boolCount, 100);

Double intCount = (Double)Context.jsToJava(sut.getGlobalScope().get("intCount", sut.getGlobalScope()), Double.class);
Double intCount = sut.getFromGlobalScope("intCount", Double.class).get();
assertEquals(500.0, intCount, 100);

Double floatCount = (Double)Context.jsToJava(sut.getGlobalScope().get("floatCount", sut.getGlobalScope()), Double.class);
Double floatCount = sut.getFromGlobalScope("floatCount", Double.class).get();
assertEquals(500.0, floatCount, 100);

}
Expand Down

0 comments on commit 23b667b

Please sign in to comment.