js compiler: Prevent octals in generated code

* src/current/compiler/js.xsl (compiler:js-number): New function to
    remove leading zeroes.
  (compile)[lv:const]: Use it.
* src/current/compiler/js-calc.xsl (compile-calc)[c:const]: Use it.
master
Mike Gerwitz 2018-03-27 11:15:50 -04:00
parent 67d0cd69ec
commit d284d75f39
2 changed files with 26 additions and 4 deletions

View File

@ -40,6 +40,7 @@
xmlns:c="http://www.lovullo.com/calc"
xmlns:lv="http://www.lovullo.com/rater"
xmlns:preproc="http://www.lovullo.com/rater/preproc"
xmlns:compiler="http://www.lovullo.com/rater/compiler"
xmlns:calc-compiler="http://www.lovullo.com/calc/compiler">
@ -453,7 +454,7 @@
-->
<template match="c:const" mode="compile-calc">
<!-- assumed to be numeric -->
<value-of select="@value" />
<sequence select="compiler:js-number( @value )" />
</template>

View File

@ -444,7 +444,7 @@
<text>, </text>
</if>
<value-of select="." />
<value-of select="compiler:js-number( . )" />
</for-each>
<text> ]</text>
</for-each>
@ -455,7 +455,7 @@
<text>, </text>
</if>
<value-of select="." />
<value-of select="compiler:js-number( . )" />
</for-each>
<text> ]; </text>
@ -470,7 +470,7 @@
<text>consts['</text>
<value-of select="@name" />
<text>'] = </text>
<value-of select="@value" />
<value-of select="compiler:js-number( @value )" />
<text>;</text>
</template>
@ -529,6 +529,27 @@
</function>
<!--
Format JS numbers such that they won't be misinterpreted as octal (if they
have leading zeroes)
-->
<function name="compiler:js-number" as="xs:string?">
<param name="src" as="xs:string?" />
<variable name="norm" as="xs:string?"
select="normalize-space( $src )" />
<!-- note that this will make 0 into an empty string! -->
<variable name="stripped" as="xs:string"
select="replace( $norm, '^0+', '' )" />
<sequence select="if ( $stripped = '' ) then
$norm
else
$stripped" />
</function>
<!--
Generate code to perform a classification