emscripten

Adam D. Ruppe destructionator at gmail.com
Sat Dec 18 13:54:36 PST 2010


Nick Sabalausky wrote:
> Also, I'm not convinced that that duplication can't be abstracted away.

I find some functions are easily copy/pasted from a server side
language out to the javascript. Though as it gets complex, it
needs more and more library support on both ends.

That's why I rarely bother trying with it in practice: writing
sharable code is easy, but just doing it on the server using the
full capabilities it has is /easier/, and its almost the same speed
as doing it in pure script anyway! The http submit and response
really doesn't take a very long time.

http://arsdnet.net/cgi-bin/noscript

Submit some stuff... not really slow. Doesn't even refresh the
whole page if your browser has iframe support, but no script.

And if your browser doesn't support iframes, it still works,
since it's just a standard form anyway.


Here's the source:

=====
import arsd.cgi;
import arsd.sha;

void main() {
	auto cgi = new Cgi;
	scope(exit) cgi.close();

	if("text" in cgi.get) {
		cgi.write(hashToString(SHA256!string(cgi.get["text"])));
		return;
	}

	cgi.write(`
		<iframe width="100%" height="30" style="border: none;" border="0" name="hash"
id="hash"></iframe>
		<form target="hash">
			Enter some text to hash: <input type="text" name="text" />
			<input type="submit" />
		</form>
	`);
}
========

Trivial.

How much effort would it have been to do this in Javascript?
JS doesn't have the same library ecosystem available to it, nor
does it have the facilities to make things as simple as languages
like D. I've seen implementations of SHA256 in javascript. Pretty
ugly. Of course, my implementation is pretty ugly too, so maybe
that's a push!

Still, the main point is just that the server can do it really
easily anyway, and then it works for everyone, script or not. Might
as well just do it the easy way on the server.

(If you want to compile it yourself, grab the referenced modules
from here: http://arsdnet.net/dcode/ and use a command line
like so:

 dmd noscript.d cgi.d sha.d -Idmd2/src/druntime/src/
dmd2/src/druntime/src/object_.d  -d

Most that spam is to convince the associative array in cgi.d to
actually link. I think there's a bug somewhere in dmd or druntime, but this works
around it.)


More information about the Digitalmars-d mailing list