MCPcopy Create free account
hub / github.com/ampproject/amphtml / Validator

Function Validator

validator/js/nodejs/index.js:208–226  ·  view source on GitHub ↗

* 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)

Source from the content-addressed store, hash-verified

206 * @constructor
207 */
208function 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.

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected