-
-
Notifications
You must be signed in to change notification settings - Fork 7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Add vm polyfill for browser #40
base: master
Are you sure you want to change the base?
Conversation
VM BROWSER.md
Outdated
@@ -0,0 +1,193 @@ | |||
# VM BROWSER |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename to doc/vm-browser.md
and format with prettier.
dist/vm.js
Outdated
.map((error) => ({ | ||
name: error.name, | ||
newError: class ErrorWithFilename extends error { | ||
constructor(message, filename, lineNumber) { | ||
super(message, filename, lineNumber); | ||
this.filename = errorFilename; | ||
} | ||
}, | ||
})) | ||
.forEach((v) => (contentWindow[v.name] = v.newError)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd prefer for..of
dist/vm.js
Outdated
const throwEvalError = () => { | ||
const msg = 'Code generation from strings disallowed for this context'; | ||
throw new EvalError(msg); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const throwEvalError = () => { | |
const msg = 'Code generation from strings disallowed for this context'; | |
throw new EvalError(msg); | |
}; | |
const ERR_EVAL = 'Code generation from strings disallowed for this context'; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use throw new Error(ERR_EVAL);
from all other places
dist/vm.js
Outdated
constructor(code, options = {}) { | ||
this.code = code; | ||
if (isFilename(options)) { | ||
options = makeOptionsFromFilename(options); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Don't rewrite arguments
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also please rebase on master
element.style.display = 'none'; | ||
element.id = id; | ||
element.name = name; | ||
element.className = IFRAME_CLASS_DEFAULT; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Use hardcode, Luke
const wrappedErrors = errors.map((error) => ({ | ||
name: error.name, | ||
newError: class ErrorWithFilename extends error { | ||
constructor(message, filename, lineNumber) { | ||
super(message, filename, lineNumber); | ||
this.filename = errorFilename; | ||
} | ||
}, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const wrappedErrors = errors.map((error) => ({ | |
name: error.name, | |
newError: class ErrorWithFilename extends error { | |
constructor(message, filename, lineNumber) { | |
super(message, filename, lineNumber); | |
this.filename = errorFilename; | |
} | |
}, | |
const wrappedErrors = errors.map((ErrorClass) => ({ | |
name: ErrorClass.name, | |
newError: class ErrorWithFilename extends ErrorClass { | |
constructor(message, filename, lineNumber) { | |
super(message, filename, lineNumber); | |
this.filename = errorFilename; | |
} | |
}, |
} | ||
}, | ||
})); | ||
for (const wrappedError of wrappedErrors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Avoid double loops over error classes collection
} | ||
}, | ||
})); | ||
for (const wrappedError of wrappedErrors) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for (const wrappedError of wrappedErrors) { | |
for (const ErrorClass of wrappedErrors) { |
}; | ||
|
||
const createIframe = (document, context, options) => { | ||
const id = `iframe-vm:${Date.now()}`; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Counter
return { | ||
id, | ||
deleteFromDocument() { | ||
if (isAttachedToDocument()) { | ||
document.body.removeChild(element); | ||
} | ||
}, | ||
updateContext() { | ||
if (isAttachedToDocument()) { | ||
for (const [key, value] of Object.entries(contentWindow)) { | ||
if (isContextKey(key) || canAddNewKey(key)) { | ||
context[key] = value; | ||
} | ||
} | ||
} | ||
}, | ||
runScript(script) { | ||
return runScript(script); | ||
}, | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class
return { | ||
updateContext() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Class
} catch (e) { | ||
deleteIframe(iframe); | ||
throw e; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
} catch (e) { | |
deleteIframe(iframe); | |
throw e; | |
} | |
} catch (error) { | |
deleteIframe(iframe); | |
throw error; | |
} |
aa135e3
to
fd357e8
Compare
fd357e8
to
fee449b
Compare
fee449b
to
f4109ae
Compare
Allows run script in iframe with your context
Add tests
npm run t
)npm run fmt
)