Skip to content

Commit 5375ccf

Browse files
committed
Rhino 1.7.10 + favor int over decimal in handlebars.js
1 parent a4174b7 commit 5375ccf

4 files changed

Lines changed: 58 additions & 4 deletions

File tree

handlebars/src/main/java/com/github/jknack/handlebars/internal/js/RhinoHandlebars.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.util.Map;
2525
import java.util.Map.Entry;
2626

27+
import static org.mozilla.javascript.Context.FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE;
28+
import org.mozilla.javascript.ContextFactory;
2729
import org.mozilla.javascript.NativeArray;
2830
import org.mozilla.javascript.NativeObject;
2931
import org.mozilla.javascript.Scriptable;
@@ -323,13 +325,27 @@ public void registerHelpers(final String filename, final String source) throws E
323325
* @return A Rhino Context.
324326
*/
325327
private org.mozilla.javascript.Context newContext() {
326-
org.mozilla.javascript.Context ctx = org.mozilla.javascript.Context.enter();
328+
final ContextFactory factory = newFactory();
329+
org.mozilla.javascript.Context ctx = factory.enterContext();
327330
ctx.setOptimizationLevel(optimizationLevel);
328331
ctx.setErrorReporter(new ToolErrorReporter(false));
329-
ctx.setLanguageVersion(org.mozilla.javascript.Context.VERSION_1_8);
332+
ctx.setLanguageVersion(org.mozilla.javascript.Context.VERSION_ES6);
330333
return ctx;
331334
}
332335

336+
private static ContextFactory newFactory() {
337+
return new ContextFactory() {
338+
@Override
339+
protected boolean hasFeature(final org.mozilla.javascript.Context cx,
340+
final int featureIndex) {
341+
if (featureIndex == FEATURE_INTEGER_WITHOUT_DECIMAL_PLACE) {
342+
return true;
343+
}
344+
return super.hasFeature(cx, featureIndex);
345+
}
346+
};
347+
}
348+
333349
/**
334350
* Creates a initialize the helpers.rhino.js scope.
335351
*

handlebars/src/test/java/com/github/jknack/handlebars/i243/Issue243.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ protected Handlebars newHandlebars() {
2828
@Test
2929
public void zeroValueForJavaScriptHelper() throws IOException {
3030
shouldCompileTo("{{#each item}}{{getIndex @index}} {{/each}}",
31-
$("item", new Object[]{10, 20, 30 }), "0.0 1.0 2.0 ");
31+
$("item", new Object[]{10, 20, 30 }), "0 1 2 ");
3232
}
3333

3434
@Test
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.github.jknack.handlebars.issues;
2+
3+
import com.github.jknack.handlebars.Handlebars;
4+
import com.github.jknack.handlebars.v4Test;
5+
import org.junit.BeforeClass;
6+
import org.junit.Test;
7+
8+
import static org.junit.Assert.assertEquals;
9+
10+
import java.io.IOException;
11+
import java.util.Arrays;
12+
13+
public class Issue614 extends v4Test {
14+
15+
@Override protected void configure(Handlebars handlebars) {
16+
try {
17+
handlebars.registerHelpers("helpers.js",
18+
"Handlebars.registerHelper('number', function(value) {\n"
19+
+ " return value;\n"
20+
+ "});\n"
21+
+ "\n"
22+
+ "Handlebars.registerHelper('len', function(array) {\n"
23+
+ " return array.length;\n"
24+
+ "});");
25+
} catch (Exception x) {
26+
throw new IllegalStateException(x);
27+
}
28+
}
29+
30+
@Test
31+
public void shouldFavorIntOverDouble() throws Exception {
32+
shouldCompileTo("{{number this}}", $("hash", 3), "3");
33+
shouldCompileTo("{{number this}}", $("hash", 3.1), "3.1");
34+
35+
shouldCompileTo("{{len this}}", $("hash", new Object[]{1, 2, 3, 4}), "4");
36+
shouldCompileTo("{{len this}}", $("hash", Arrays.asList(1, 2, 3, 4)), "4");
37+
}
38+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@
9393
<dependency>
9494
<groupId>org.mozilla</groupId>
9595
<artifactId>rhino</artifactId>
96-
<version>1.7R4</version>
96+
<version>1.7.10</version>
9797
</dependency>
9898

9999
<!-- Logging System -->

0 commit comments

Comments
 (0)