imcplugin demo: Extend to support stat() call
[nativeclient.git] / tests / nacl_js_lib.js
blob0a065ca6dc5662128e90fdb7745534d3f7278b28
1 // JavaScript Library for Nacl Tests and Demos
3 function NaclLib(modules, statusfield, num_retries) {
4 this.modules_ = modules;
5 this.statusfield_ = statusfield;
6 this.status_ = "WAIT";
7 this.message_ = "";
8 this.handler_ = null;
9 this.retries_ = num_retries;
13 NaclLib.prototype.getStatus = function() {
14 return this.status_;
18 NaclLib.prototype.getMessage = function() {
19 return this.message_;
23 NaclLib.prototype.cleanUp = function() {
24 if (this.handler_) {
25 clearInterval(this._handler);
30 NaclLib.prototype.setStatus = function() {
31 this.statusfield_.innerHTML =
32 this.status_ + ": " + this.message_;
36 NaclLib.prototype.setStatusWait = function(message) {
37 this.status_ = "WAIT";
38 this.message_ = message;
39 this.setStatus()
43 NaclLib.prototype.setStatusError = function(message) {
44 this.status_ = "ERROR";
45 this.message_ = message;
46 this.setStatus()
50 NaclLib.prototype.setStatusSuccess = function(message) {
51 this.status_ = "SUCCESS";
52 this.message_ = message;
53 this.setStatus()
57 NaclLib.prototype.numModulesReady = function(modules) {
58 var count = 0;
59 for (var i = 0; i < modules.length; i++) {
60 if (modules[i].__moduleReady == 1) {
61 count += 1;
64 return count;
68 NaclLib.prototype.areTherePluginProblems = function(modules) {
69 for (var i = 0; i < modules.length; i++) {
70 if (modules[i].__moduleReady == undefined) return 1;
72 return 0;
76 NaclLib.prototype.checkModuleReadiness = function() {
77 var num_ready = this.numModulesReady(this.modules_);
79 this.setStatusWait("" + this.retries_ + ": Loaded " +
80 num_ready + "/" + this.modules_.length + " modules");
82 if (this.modules_.length == num_ready) {
83 this.cleanUp();
84 var result;
85 try {
86 result = this.test();
87 } catch(e) {
88 this.setStatusError(e);
89 return;
92 if (result == "") {
93 this.setStatusSuccess("");
94 } else {
95 this.setStatusError(result);
97 return;
100 if (this.areTherePluginProblems(this.modules_)) {
101 this.cleanUp();
102 this.setStatusError("The Native Client plugin was unable to load");
103 return;
106 if (this.retries_ == 0) {
107 this.cleanUp();
108 this.setStatusError("The Native Client modules are loading too slowly");
109 return;
112 this.retries_ -= 1;
116 // Workaround for JS inconsistent scoping behavior
117 function wrapperForCheckModuleReadiness(that) {
118 that.checkModuleReadiness();
122 NaclLib.prototype.waitForModulesAndRunTests = function() {
123 this.handler_ = setInterval(wrapperForCheckModuleReadiness, 100, this);