Skip to content
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

Use 'import' instead of 'eval' for worklet code #33

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 20 additions & 26 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,33 +59,27 @@ if (typeof AudioWorkletNode !== 'function' || !("audioWorklet" in AudioContext.p
this.$$context = audioContext;
}

addModule (url, options) {
return fetch(url).then(r => {
if (!r.ok) throw Error(r.status);
return r.text();
}).then(code => {
const context = {
sampleRate: this.$$context.sampleRate,
currentTime: this.$$context.currentTime,
AudioWorkletProcessor () {
this.port = nextPort;
},
registerProcessor: (name, Processor) => {
const processors = getProcessorsForContext(this.$$context);
processors[name] = {
realm,
context,
Processor,
properties: Processor.parameterDescriptors || []
};
}
};
addModule (url) {
const context = {
sampleRate: this.$$context.sampleRate,
currentTime: this.$$context.currentTime,
AudioWorkletProcessor () {
this.port = nextPort;
},
registerProcessor: (name, Processor) => {
const processors = getProcessorsForContext(this.$$context);
processors[name] = {
realm,
context,
Processor,
properties: Processor.parameterDescriptors || []
};
}
};

context.self = context;
const realm = new Realm(context, document.documentElement);
realm.exec(((options && options.transpile) || String)(code));
return null;
});
context.self = context;
const realm = new Realm(context, document.documentElement);
return realm.exec(url).then(() => Promise.resolve());
}
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/realm.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ export function Realm (scope, parentElement) {
const script = doc.createElement('script');
script.appendChild(doc.createTextNode(
`function $hook(self,console) {"use strict";
${vars};return function() {return eval(arguments[0])}}`
${vars};return function() {return import(arguments[0])}}`
));
doc.body.appendChild(script);
this.exec = win.$hook.call(scope, scope, console);
Expand Down