Skip to content

Interacting with xmllint in javascript

jallwine edited this page Apr 5, 2013 · 2 revisions

You can interact with compiled code in different ways. It's all outlined here: https://github.com/kripken/emscripten/wiki/Interacting-with-code

In this example, the validateXML function called with the schema and xml values from the textareas. validateXML is a javascript function that simply places the strings you pass it into a place where emscripten can fake reading them in as files. I stole it from the xml.js example. When you compile an executable (in this case xmllint), it knows to run the main function like in C. Emscripten looks for a Module object with various hooks for passing arguments, a stdout callback, and also a javascript callback that you can have it run before running the compiled code.

This code is added in in the last step of the compilation stage using the --pre-js command line flag.

Module['preRun'] = function() {
    FS.createDataFile('/', 'test.xml', Module['intArrayFromString'](Module['xml']), true, true);
    FS.createDataFile('/', 'test.xsd', Module['intArrayFromString'](Module['schema']), true, true);
};
Module.arguments = ['--noout', '--schema', 'test.xsd', 'test.xml'];
Module['return'] = '';
Module['print'] = function(text) {
    Module['return'] += text + '\n';
};

You can see the arguments array which holds the command line flags that will be passed to xmllint. Two of those arguments are file names. You can see the preRun callback creates those data files using FS.createDataFile which is provided by emscripten. It pulls the data from the Module object: Module['xml'] and Module['schema']. Those values are set by validateXML:

function validateXML(xml, schema) {
    var Module = {
        xml: xml,
        schema: schema
    };
    // this section is a long one line, minified version of xmllint
    function da(b){throw b}var ea=void 0,ga=!0,ja=null,xa=!1;function Ba(b){return(function(){return 
    return Module.return;
}
Clone this wiki locally