Skip to content

Commit

Permalink
Merge branch 'develop' of github.com:bThink-BGU/BPjs into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
michbarsinai committed Dec 30, 2020
2 parents fe4d3c3 + 7943cc4 commit cee6fb6
Show file tree
Hide file tree
Showing 5 changed files with 156 additions and 84 deletions.
95 changes: 84 additions & 11 deletions dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.github.bthink-bgu</groupId>
<artifactId>BPjs</artifactId>
<name>BPjs</name>
<version>0.10.7</version>
<version>0.11.0-SNAPSHOT</version>
<description>Provides runtime and analysis for behavioral programs written in
JavaScript. It can run stand-alone (from the commmandline) or be
embedded in larger JVM-based systems.</description>
Expand Down Expand Up @@ -42,11 +42,18 @@
<groupId>org.eluder.coveralls</groupId>
<artifactId>coveralls-maven-plugin</artifactId>
<version>4.3.0</version>
<dependencies>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</plugin>
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>0.8.5</version>
<version>0.8.6</version>
<executions>
<execution>
<id>prepare-agent</id>
Expand All @@ -70,14 +77,6 @@
<plugin>
<artifactId>maven-javadoc-plugin</artifactId>
<version>2.10.4</version>
<executions>
<execution>
<id>attach-javadocs</id>
<goals>
<goal>jar</goal>
</goals>
</execution>
</executions>
<configuration>
<source>8</source>
</configuration>
Expand Down Expand Up @@ -179,7 +178,7 @@
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13</version>
<version>4.13.1</version>
<scope>test</scope>
</dependency>
<dependency>
Expand All @@ -194,6 +193,78 @@
<version>1.8</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.44.0</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>selenium-chrome-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-htmlunit-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-firefox-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-ie-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-safari-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>selenium-support</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>webbit</artifactId>
<groupId>org.webbitserver</groupId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.opera</groupId>
<artifactId>operadriver</artifactId>
<version>1.5</version>
<scope>test</scope>
<exclusions>
<exclusion>
<artifactId>selenium-remote-driver</artifactId>
<groupId>org.seleniumhq.selenium</groupId>
</exclusion>
<exclusion>
<artifactId>operalaunchers</artifactId>
<groupId>com.opera</groupId>
</exclusion>
<exclusion>
<artifactId>protobuf-java</artifactId>
<groupId>com.google.protobuf</groupId>
</exclusion>
<exclusion>
<artifactId>guava</artifactId>
<groupId>com.google.guava</groupId>
</exclusion>
<exclusion>
<artifactId>commons-jxpath</artifactId>
<groupId>commons-jxpath</groupId>
</exclusion>
<exclusion>
<artifactId>commons-exec</artifactId>
<groupId>org.apache.commons</groupId>
</exclusion>
<exclusion>
<artifactId>ini4j</artifactId>
<groupId>org.ini4j</groupId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<distributionManagement>
<repository>
Expand All @@ -206,9 +277,11 @@
</snapshotRepository>
</distributionManagement>
<properties>
<javax.activation.version>1.2.0</javax.activation.version>
<exec.mainClass>il.ac.bgu.cs.bp.bpjs.mains.BPJsCliRunner</exec.mainClass>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<jaxb.api.version>2.3.0</jaxb.api.version>
</properties>
</project>

61 changes: 13 additions & 48 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/execution/jsproxy/BpLog.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,11 @@
*/
package il.ac.bgu.cs.bp.bpjs.execution.jsproxy;

import il.ac.bgu.cs.bp.bpjs.internal.ScriptableUtils;
import il.ac.bgu.cs.bp.bpjs.model.BProgram;

import java.text.MessageFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Set;
import static java.util.stream.Collectors.joining;
import java.util.stream.IntStream;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.NativeArray;
import org.mozilla.javascript.ScriptableObject;

/**
* Simple logging mechanism for {@link BProgram}s.
Expand All @@ -46,21 +41,23 @@ public enum LogLevel {
}
LogLevel level = LogLevel.Info;

public void warn(Object msg) {
log(LogLevel.Warn, msg);
public void warn(Object msg, Object ...args) {
log(LogLevel.Warn, msg, args);
}

public void info(Object msg) {
log(LogLevel.Info, msg);
public void info(Object msg, Object ...args) {
log(LogLevel.Info, msg, args);
}

public void fine(Object msg) {
log(LogLevel.Fine, msg);
public void fine(Object msg, Object ...args) {
log(LogLevel.Fine, msg, args);
}

public void log(LogLevel lvl, Object msg) {
public void log(LogLevel lvl, Object msg, Object ...args) {
if (level.compareTo(lvl) >= 0) {
System.out.println("[BP][" + lvl.name() + "] " + stringify(msg));
System.out.println("[BP][" + lvl.name() + "] " + MessageFormat.format(
ScriptableUtils.stringify(msg),
Arrays.stream(args).map(ScriptableUtils::stringify).toArray()));
}
}

Expand All @@ -74,36 +71,4 @@ public String getLevel() {
return level.name();
}

private String stringify( Object o ) {
if ( o == null ) return "<null>";
if ( o instanceof String ) return "\""+o + "\"";
if ( o instanceof ConsString ) return "\"" + ((ConsString)o).toString() + "\"";
if ( o instanceof NativeArray ) {
NativeArray arr = (NativeArray) o;
return arr.getIndexIds().stream().map( id -> id + ":" + stringify(arr.get(id)) ).collect(joining(" | ", "[JS_Array ", "]"));
}
if ( o instanceof ScriptableObject ) {
ScriptableObject sob = (ScriptableObject) o;
return Arrays.stream(sob.getIds()).map( id -> id + ": " + stringify(sob.get(id)) ).collect( joining("\n", "{JS_Obj ", "}"));
}
if ( o instanceof Object[] ) {
Object[] objArr = (Object[]) o;
return IntStream.range(0, objArr.length).mapToObj(idx -> stringify(objArr[idx])).collect(joining("|","[Java_Array ", "]"));
}
if ( o instanceof Map ) {
Map<?,?> mp = (Map<?,?>) o;
return mp.entrySet().stream().map( e -> e.getKey()+"->" + stringify(e.getValue())).collect(joining(",","{Map ", "}"));
}
if ( o instanceof List ) {
List<?> ls = (List<?>) o;
return ls.stream().map(this::stringify).collect(joining(", ","<List ", ">"));
}
if ( o instanceof Set ) {
Set<?> ls = (Set<?>) o;
return ls.stream().map(this::stringify).collect(joining(", ","{Set ", "}"));
}
return o.toString();

}

}
26 changes: 19 additions & 7 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/execution/jsproxy/MapProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@
*/
package il.ac.bgu.cs.bp.bpjs.execution.jsproxy;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import il.ac.bgu.cs.bp.bpjs.internal.Pair;

import java.util.*;
import java.util.function.BiFunction;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
* A Proxy to a map.Stores changes to the proxied map, but does not change it.
Expand All @@ -36,7 +38,7 @@
* @param <V> Type of values in the map.
*/
public class MapProxy<K,V> implements java.io.Serializable {

public static abstract class Modification<VV> implements java.io.Serializable {
public static final Modification DELETE = new Modification(){};
}
Expand Down Expand Up @@ -95,7 +97,17 @@ public void put(K key, V value ) {
public void remove(K key) {
modifications.put(key, Modification.DELETE);
}


public Map<K, V> filter(BiFunction<K, V, Boolean> func) {
Map<K, V> result = modifications.entrySet().stream()
.filter(entry -> entry.getValue() != Modification.DELETE && func.apply(entry.getKey(), ((PutValue<V>) entry.getValue()).value))
.collect(Collectors.toMap(Map.Entry::getKey, entry -> ((PutValue<V>) entry.getValue()).value));
result.putAll(seed.entrySet().stream()
.filter(entry -> !modifications.containsKey(entry.getKey()) && func.apply(entry.getKey(), entry.getValue()))
.collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));
return result;
}

public V get( K key ) {
if ( modifications.containsKey(key) ) {
Modification<V> m = modifications.get(key);
Expand Down
54 changes: 39 additions & 15 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/internal/ScriptableUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@
*/
package il.ac.bgu.cs.bp.bpjs.internal;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import java.util.*;

import static java.util.stream.Collectors.joining;
import java.util.stream.Stream;
import org.mozilla.javascript.ConsString;
import org.mozilla.javascript.Scriptable;
import org.mozilla.javascript.ScriptableObject;

import java.util.stream.IntStream;

import org.mozilla.javascript.*;

/**
* Some methods to make working with {@link Scriptable} easier.
Expand All @@ -49,13 +47,7 @@ public static Scriptable getPenultiamteParent( Scriptable aScope ) {
}

public static String toString( Scriptable aScope ) {
if ( aScope == null ) return "<null>";
if ( aScope instanceof ScriptableObject ) {
ScriptableObject sob = (ScriptableObject) aScope;
return Stream.of(sob.getIds())
.map( k -> k.toString() + ":" + Objects.toString(sob.get(k)) )
.collect( joining(",", "{", "}") );
} else return aScope.toString();
return stringify(aScope);
}

/**
Expand Down Expand Up @@ -99,6 +91,38 @@ public static boolean jsScriptableObjectEqual(ScriptableObject o1, ScriptableObj
return o1Ids.equals(o2Ids) &&
o1Ids.stream().allMatch(id -> jsEquals(o1.get(id), o2.get(id)));
}

public static String stringify( Object o ) {
if ( o == null ) return "<null>";
if ( o instanceof String ) return "\""+o + "\"";
if ( o instanceof ConsString ) return "\"" + ((ConsString)o).toString() + "\"";
if ( o instanceof NativeArray) {
NativeArray arr = (NativeArray) o;
return arr.getIndexIds().stream().map( id -> id + ":" + stringify(arr.get(id)) ).collect(joining(" | ", "[JS_Array ", "]"));
}
if ( o instanceof ScriptableObject ) {
ScriptableObject sob = (ScriptableObject) o;
return Arrays.stream(sob.getIds()).map( id -> id + ": " + stringify(sob.get(id)) ).collect( joining(", Cha", "{JS_Obj ", "}"));
}
if ( o instanceof Object[] ) {
Object[] objArr = (Object[]) o;
return IntStream.range(0, objArr.length).mapToObj(idx -> stringify(objArr[idx])).collect(joining("|","[Java_Array ", "]"));
}
if ( o instanceof Map) {
Map<?,?> mp = (Map<?,?>) o;
return mp.entrySet().stream().map( e -> e.getKey()+"->" + stringify(e.getValue())).collect(joining(",","{Map ", "}"));
}
if ( o instanceof List ) {
List<?> ls = (List<?>) o;
return ls.stream().map(ScriptableUtils::stringify).collect(joining(", ","<List ", ">"));
}
if ( o instanceof Set ) {
Set<?> ls = (Set<?>) o;
return ls.stream().map(ScriptableUtils::stringify).collect(joining(", ","{Set ", "}"));
}
return o.toString();

}

/**
* Deep-hash of an object.
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/il/ac/bgu/cs/bp/bpjs/model/BEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ public Object getData() {
* @return String representation of {@code data}.
*/
private String dataToString( Object data ) {
if ( data == null ) return "<null>";
return ( data instanceof Scriptable ) ? ScriptableUtils.toString((Scriptable) data)
: Objects.toString(data);
return ScriptableUtils.stringify(data);
}

@Override
Expand Down

0 comments on commit cee6fb6

Please sign in to comment.