Skip to content

Commit

Permalink
gut addModule and reInit
Browse files Browse the repository at this point in the history
gut addModule and reInit tests
  • Loading branch information
Albert-Jan Nijburg committed Nov 24, 2015
1 parent d4def50 commit 612aa23
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 122 deletions.
3 changes: 2 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
"printErr": true,
"snarf": true,
"read": true,
"loadRelativeToScript": true
"loadRelativeToScript": true,
"pypyjs": true
}
}
3 changes: 0 additions & 3 deletions lib/tests/test_module.py

This file was deleted.

99 changes: 1 addition & 98 deletions src/pypyjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -408,20 +408,12 @@ top_level_scope = main"`;
});
}

pypyjs.prototype.inJsModules = null;

// A simple file-fetching wrapper around XMLHttpRequest,
// that treats paths as relative to the pypyjs.js root url.
//
pypyjs.prototype.fetch = function fetch(relpath, responseType) {
const rootURL = this.rootURL || pypyjs.rootURL;

if (this.inJsModules && this.inJsModules[relpath]) {
return new Promise((resolve) => {
resolve({ responseText: this.inJsModules[relpath] });
});
}

// For the web, use XMLHttpRequest.
if (typeof XMLHttpRequest !== 'undefined') {
return new Promise((resolve, reject) => {
Expand Down Expand Up @@ -471,32 +463,6 @@ pypyjs.prototype.fetch = function fetch(relpath, responseType) {
});
};

// add a Module to the vm from a file so you can import it later. Uses a path
// relative to pypyjs.
pypyjs.prototype.addModuleFromFile = function addModuleFromFile(name, file) {
return this.fetch(file).then((data) => this.addModule(name, data.responseText));
};

// add a Module to the vm from javascript. It will automatically write it to
// the "disk" when you import it in your code later.
pypyjs.prototype.addModule = function addModule(name, source) {
return this.findImportedNames(source).then((imports) => {
// keep track of any modules that have been previously loaded
if (this._loadedModules[name]) {
this._modulesToReset[name] = true;
this._loadedModules[name] = null;
}
this._allModules[name] = {
file: `${name}.py`,
imports
};
if (!this.inJsModules) {
this.inJsModules = {};
}
this.inJsModules[`modules/${name}.py`] = source;
});
};

function _blockIndent(code, indent) {
return code.replace(/\n/g, `\n${indent}`);
}
Expand Down Expand Up @@ -592,29 +558,6 @@ pypyjs.prototype.exec = function exec(code, options) {
});
}

// if any modules have been re-added then we need to remove them from
// sys.modules which clears them from memory and allows them to be reloaded
// from the emscripten file system
if (Object.keys(this._modulesToReset).length) {
// construct python code to remove module from sys.modules:
// ```python
// import sys
// if 'foo' in sys.modules: del(sys.modules['foo'])
// ```
const modulesToLoad =
Object.keys(this._modulesToReset)
.map(mod => `if '${mod}' in sys.modules: del(sys.modules['${mod}'])`);

preCode = `
try:
import sys
${modulesToLoad.join('\n ')}
except:
raise SystemError('Failed to reload custom modules')`;

this._modulesToReset = {};
}

let _code;

if (options && options.file) {
Expand All @@ -639,41 +582,6 @@ except:
});
};

// Method to reinitialize the global scope without reloading the vm.
pypyjs.prototype.reInit = function reInit() {
const Module = this._module;
return new Promise((resolve) => {
// code to exec
const initCode =
`del(sys.modules['__main__'])
top_level_scope = {'__name__': '__main__', '__package__': None}
main = types.ModuleType('__main__')
main.__dict__.update(top_level_scope)
sys.modules['__main__'] = main
top_level_scope = main`;

// make c string
let code = Module.intArrayFromString(initCode);
// alloc
code = Module.allocate(code, 'i8', Module.ALLOC_NORMAL);

if (!code) {
throw new pypyjs.Error('Failed to allocate memory');
}

// exec
const res = Module._pypy_execute_source(code);

if (res < 0) {
throw new pypyjs.Error('Failed to execute python code');
}

Module._free(code);

resolve();
});
};

// Method to evaluate an expression.
//
// This method evaluates an expression and returns its value (assuming the
Expand Down Expand Up @@ -708,13 +616,8 @@ pypyjs.prototype.eval = function evaluate(expr) {
// This fetches the named file and passes it to the VM for execution.
//
pypyjs.prototype.execfile = function execfile(filename) {
const _path = this.inJsModules[`modules/${filename}`]
? `modules/${filename}`
: filename;

return this.fetch(_path).then((xhr) => {
return this.fetch(filename).then((xhr) => {
const code = xhr.responseText;

return this.exec(code, { file: `/lib/pypyjs/lib_pypy/${filename}` });
});
};
Expand Down
20 changes: 0 additions & 20 deletions src/tests/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
// A very minimal testsuite for the PyPy.js shell code.
// We should do something a lot nicer than this...
//
let pypyjs;

if (typeof pypyjs === 'undefined') {
if (typeof require !== 'undefined') {
pypyjs = require('../pypyjs.js');
Expand Down Expand Up @@ -107,17 +105,6 @@ pypyjsTestResult
throw new Error('multi-line import didn\'t work');
}
})
// add module from js that imports modules
.then(() => {
return vm.addModule('testmodule', `
import time
import sys
import os
assert time.time() > 0`).then(() => vm.exec('import testmodule'));
})
.then(() => {
return vm.addModuleFromFile('testmodule2', 'tests/test_module.py').then(() => vm.exec('import testmodule2'));
})
// Check that you can create additional VMs using `new`
.then(() => {
const vm2 = new pypyjs();
Expand All @@ -133,13 +120,6 @@ assert time.time() > 0`).then(() => vm.exec('import testmodule'));
if (typeof y !== 'undefined') {
throw new Error('name should have been undefined in new VM');
}
})
.then(() => vm2.reInit())
.then(() => vm2.get('x'))
.then((x) => {
if (typeof x !== 'undefined') {
throw new Error('name should have been undefined in new VM');
}
});
})

Expand Down

0 comments on commit 612aa23

Please sign in to comment.