Skip to content

Commit

Permalink
Avoid importing/using mathjs functions during evaluation
Browse files Browse the repository at this point in the history
  • Loading branch information
lubiedo committed Jul 20, 2021
1 parent 77398c7 commit 86a4e60
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions js/searchbar/calculatorPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,28 @@

const { clipboard } = require('electron')
const searchbarPlugins = require('searchbar/searchbarPlugins.js')
const math = require('mathjs')
const { create, all } = require('mathjs')

const math = create(all)
const mathEval = math.evaluate
const disabledFunc = () => { }
const disabledFuncs = {
import: disabledFunc,
createUnit: disabledFunc,
evaluate: disabledFunc,
parse: disabledFunc,
simplify: disabledFunc,
derivative: disabledFunc
}

math.import(disabledFuncs, { override: true })

function doMath (text, input, event) {
searchbarPlugins.reset('calculatorPlugin')

var result
try {
result = math.evaluate(text)
result = mathEval(text)
} catch (e) { return }

searchbarPlugins.addResult('calculatorPlugin', {
Expand Down Expand Up @@ -40,6 +54,12 @@ function initialize () {
return false
}

// avoid returning disable functions
for (const fn of Object.keys(disabledFuncs)) {
if (text.indexOf(fn) !== -1) {
return false
}
}
try { math.parse(text) } catch (e) { return false }
return true
},
Expand Down

0 comments on commit 86a4e60

Please sign in to comment.