jsparser: store math & js parsers in the same prop, add default context to avoid errors

This commit is contained in:
jean-emmanuel 2019-04-07 09:51:15 +02:00
parent 2ed61790eb
commit 258fc7c750
4 changed files with 51 additions and 11 deletions

View file

@ -136,7 +136,6 @@ class Widget extends EventEmitter {
this.props = options.props
this.errors = {}
this.parsers = {}
this.jsparsers = {}
this.parent = options.root ? widgetManager : options.parent
this.parentNode = options.parentNode
this.hash = nanoid('abcdefghijklmnopqrstuvwxyABCDEFGHIJKLMNOPQRSTUVWXYZ', 10)
@ -418,7 +417,7 @@ class Widget extends EventEmitter {
originalPropName = originalPropName || propName
var variables = {},
mathscope = context || {},
mathscope = context || this.constructor.parsersContexts[propName] || {},
varnumber = 999
if (typeof propValue == 'string') {
@ -600,13 +599,15 @@ class Widget extends EventEmitter {
try {
propValue = propValue.replace(/JS\{\{([\s\S]*)\}\}/g, (m, code)=>{
if (!this.jsparsers[code]) this.jsparsers[code] = evaljs(code)
let r = this.jsparsers[code](mathscope)
if (!this.parsers[code]) this.parsers[code] = evaljs(code)
let r = this.parsers[code](mathscope)
if (r === undefined) r = ''
return typeof r !== 'string' ? JSON.stringify(r) : r
})
} catch (err) {
var stackline = err.stack ? err.stack.match(/>:([0-9]+):[0-9]+/) : '',
@ -910,6 +911,8 @@ class Widget extends EventEmitter {
}
Widget.parsersContexts = {}
Widget.dynamicProps = [
'top',
'left',

View file

@ -152,6 +152,15 @@ class Keys extends Widget {
}
Keys.parsersContexts.keydown = Keys.parsersContexts.keyup = {
key: 'x',
code: 'x',
ctrl: false,
alt: false,
shift: false,
super: false
}
Keys.dynamicProps = Keys.prototype.constructor.dynamicProps.concat(
'keydown',
'keyup',

View file

@ -89,6 +89,16 @@ class Script extends Widget {
}
Script.parsersContexts.condition = {
value: ''
}
Script.parsersContexts.script = {
value: '',
send: ()=>{},
set: ()=>{}
}
Script.dynamicProps = Script.prototype.constructor.dynamicProps.concat(
'condition',
'script'

View file

@ -111,18 +111,19 @@ module.exports = {
var sandbox = document.createElement('iframe'),
loopProtect = require('loop-protect')
sandbox.style.display = 'none'
sandbox.sandbox = 'allow-scripts allow-same-origin'
document.body.appendChild(sandbox)
// init infinite loop guard
loopProtect.alias = '__protect'
loopProtect.hit = function(line){
throw 'Potential infinite loop found on line ' + line
}
sandbox.style.display = 'none'
sandbox.sandbox = 'allow-scripts allow-same-origin'
document.body.appendChild(sandbox)
sandbox.contentWindow.parsers = {}
sandbox.contentWindow.__protect = loopProtect
function evaljs(code) {
function evaljs(code, defaultContext) {
sandbox.contentWindow.parsers[code] = sandbox.contentWindow.Function(
loopProtect('"use strict";' + code)
@ -131,11 +132,28 @@ module.exports = {
return (context)=>{
for (var k in context) {
var ret, err, k
// set context
for (k in context) {
sandbox.contentWindow[k] = context[k]
}
return sandbox.contentWindow.parsers[code]()
// evaluate
try {
ret = sandbox.contentWindow.parsers[code]()
} catch(e) {
err = e
}
// clean context
for (k in context) {
delete sandbox.contentWindow[k]
}
if (err) throw err
return ret
}