* The validator instance is a proxy object to a precompiled * validator_wasm.js script - in practice the script was either downloaded * from 'https://cdn.ampproject.org/v0/validator_wasm.js' or read from a * local file. * @param {string} scriptContents * @throws {!Error} * @constructor
(scriptContents)
| 206 | * @constructor |
| 207 | */ |
| 208 | function Validator(scriptContents) { |
| 209 | // The 'sandbox' is a Javascript object (dictionary) which holds |
| 210 | // the results of evaluating the validatorJs / scriptContents, so |
| 211 | // basically, it holds functions, prototypes, etc. As a |
| 212 | // side-effect of evaluating, the VM will compile this code and |
| 213 | // it's worth holding onto it. Hence, this validate function is |
| 214 | // reached via 2 codepaths - either the sandbox came from the |
| 215 | // cache, precompiledByValidatorJs - or we just varructed it |
| 216 | // after downloading and evaluating the script. The API is fancier |
| 217 | // here, vm.Script / vm.createContext / vm.runInContext and all |
| 218 | // that, but it's quite similar to a Javascript eval. |
| 219 | this.sandbox = vm.createContext(); |
| 220 | try { |
| 221 | new vm.Script(scriptContents).runInContext(this.sandbox); |
| 222 | } catch (error) { |
| 223 | throw new Error('Could not instantiate validator_wasm.js - ' + |
| 224 | error.message); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | /** |
| 229 | * Initialize the validator. |
nothing calls this directly
no outgoing calls
no test coverage detected