Last active
March 18, 2019 12:17
-
-
Save filips123/8d6135debb763374fac5078bcee28542 to your computer and use it in GitHub Desktop.
Iodide with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const PHP_MODULE = 'https://oraoto.github.io/pib/php.js' | |
const loadResource = url => new Promise((resolve) => { | |
const head = document.getElementsByTagName('head')[0] | |
const theScript = document.createElement('script') | |
theScript.src = url; | |
theScript.onload = () => { | |
resolve() | |
}; | |
head.appendChild(theScript) | |
}) | |
loadResource(PHP_MODULE) | |
.then(() => { | |
window.php = {} | |
window.php.evaluate = code => { | |
code = code.replace(/^\s*<\?php/, '') | |
code = code + '\necho PHP_EOL;' | |
let ret = module.ccall('pib_eval', 'number', ['string'], [code]) | |
} | |
window.php.export = (...arguments) => { | |
if (arguments.length > 1) { | |
text = Array.prototype.slice.call(arguments).join(' ') | |
} else { | |
text = arguments | |
} | |
const elem = window.iodide.output.element('div') | |
elem.innerHTML = text | |
} | |
const options = { | |
print: window.php.export, | |
printErr: window.php.export, | |
} | |
const module = PHP(options) | |
window.iodide.exportPHP = (variableName, variable) => { | |
const out = variable | |
window[variableName] = out; | |
} | |
window.iodide.addOutputHandler({ | |
shouldHandle: val => true, | |
render: (val) => { | |
const elem = document.createElement('div') | |
elem.innerHTML = val | |
return elem | |
}, | |
}); | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%% plugin | |
{ | |
"languageId": "php", | |
"displayName": "PHP", | |
"codeMirrorMode": "php", | |
"keybinding": "php", | |
"url": "https://gist.githubusercontent.com/filips123/8d6135debb763374fac5078bcee28542/raw/evaluate-php.js", | |
"module": "php", | |
"evaluator": "evaluate", | |
"pluginType": "language" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment