Skip to content

Commit ff46726

Browse files
committed
Add interoperability hack to npm for functions that were built for browsers. Fixes locutusjs#145
1 parent 7226b64 commit ff46726

File tree

6 files changed

+60
-40
lines changed

6 files changed

+60
-40
lines changed

bin/phpjs.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,19 @@ cli.parse({
2121
var PhpjsUtil = phpjsutil({
2222
injectDependencies: ['ini_set', 'ini_get'],
2323
equal : equal,
24-
debug : cli.debug
24+
debug : cli.debug,
25+
globals : {
26+
'XMLHttpRequest': '{}',
27+
'window': '{' +
28+
'document: {' +
29+
'lastModified: 1388954399,' +
30+
'getElementsByTagName: function(){return [];}' +
31+
'},' +
32+
'location: {' +
33+
'href: ""' +
34+
'}' +
35+
'}',
36+
}
2537
});
2638

2739
// Environment-specific file opener. function name needs to
@@ -152,6 +164,11 @@ cli.buildnpm = function(args, options) {
152164
fs.appendFileSync(options.output, '// \n');
153165
fs.appendFileSync(options.output, '// Make function changes in ./functions and \n');
154166
fs.appendFileSync(options.output, '// generator changes in ./lib/phpjsutil.js \n');
167+
168+
for (var global in PhpjsUtil.globals) {
169+
fs.appendFileSync(options.output, 'exports.' + global + ' = ' + PhpjsUtil.globals[global] + ';\n');
170+
}
171+
155172
self.glob(pattern, function (err, params, file) {
156173
if (err) {
157174
return self.error('Could not glob for ' + pattern + '. ' + err);

build/npm.js

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
//
44
// Make function changes in ./functions and
55
// generator changes in ./lib/phpjsutil.js
6+
exports.XMLHttpRequest = {};
7+
exports.window = {document: {lastModified: 1388954399,getElementsByTagName: function(){return [];}},location: {href: ""}};
68

79
exports.array = function () {
810
try {
@@ -1229,23 +1231,6 @@ exports.array_pop = function (inputArr) {
12291231
}
12301232
};
12311233

1232-
exports.array_product = function (input) {
1233-
var idx = 0,
1234-
product = 1,
1235-
il = 0;
1236-
1237-
if (Object.prototype.toString.call(input) !== '[object Array]') {
1238-
return null;
1239-
}
1240-
1241-
il = input.length;
1242-
while (idx < il) {
1243-
product *= (!isNaN(input[idx]) ? input[idx] : 0);
1244-
idx++;
1245-
}
1246-
return product;
1247-
};
1248-
12491234
exports.array_push = function (inputArr) {
12501235
var i = 0,
12511236
pr = '',
@@ -1278,6 +1263,23 @@ exports.array_push = function (inputArr) {
12781263
return len + i - 1;
12791264
};
12801265

1266+
exports.array_product = function (input) {
1267+
var idx = 0,
1268+
product = 1,
1269+
il = 0;
1270+
1271+
if (Object.prototype.toString.call(input) !== '[object Array]') {
1272+
return null;
1273+
}
1274+
1275+
il = input.length;
1276+
while (idx < il) {
1277+
product *= (!isNaN(input[idx]) ? input[idx] : 0);
1278+
idx++;
1279+
}
1280+
return product;
1281+
};
1282+
12811283
exports.array_rand = function (input, num_req) {
12821284
var indexes = [];
12831285
var ticks = num_req || 1;
@@ -8242,11 +8244,6 @@ exports.is_object = function (mixed_var) {
82428244
return mixed_var !== null && typeof mixed_var === 'object';
82438245
};
82448246

8245-
exports.is_scalar = function (mixed_var) {
8246-
return (/boolean|number|string/)
8247-
.test(typeof mixed_var);
8248-
};
8249-
82508247
exports.is_resource = function (handle) {
82518248
var getFuncName = function(fn) {
82528249
var name = (/\W*function\s+([\w\$]+)\s*\(/)
@@ -8260,6 +8257,11 @@ exports.is_resource = function (handle) {
82608257
'PHPJS_Resource');
82618258
};
82628259

8260+
exports.is_scalar = function (mixed_var) {
8261+
return (/boolean|number|string/)
8262+
.test(typeof mixed_var);
8263+
};
8264+
82638265
exports.is_string = function (mixed_var) {
82648266
return (typeof mixed_var === 'string');
82658267
};
@@ -10526,7 +10528,11 @@ exports.setlocale = function (category, locale) {
1052610528
};
1052710529
// END STATIC
1052810530
// BEGIN REDUNDANT
10529-
this.php_js = this.php_js || {};
10531+
try {
10532+
this.php_js = this.php_js || {};
10533+
} catch (e) {
10534+
this.php_js = {};
10535+
}
1053010536

1053110537
var phpjs = this.php_js;
1053210538

functions/strings/setlocale.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,11 @@ function setlocale(category, locale) {
9595
};
9696
// END STATIC
9797
// BEGIN REDUNDANT
98-
this.php_js = this.php_js || {};
98+
try {
99+
this.php_js = this.php_js || {};
100+
} catch (e) {
101+
this.php_js = {};
102+
}
99103

100104
var phpjs = this.php_js;
101105

@@ -343,4 +347,4 @@ function setlocale(category, locale) {
343347
this.php_js.localeCategories[category] = locale;
344348
}
345349
return locale;
346-
}
350+
}

functions/strings/str_word_count.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ function str_word_count(str, format, charlist) {
1313
// returns 2: {0: 'Hello', 6: 'fri', 10: 'nd', 14: "you're", 29: 'looking', 46: 'good', 51: 'today'}
1414
// example 3: str_word_count("Hello fri3nd, you're\r\n looking good today!", 1, '\u00e0\u00e1\u00e3\u00e73');
1515
// returns 3: ['Hello', 'fri3nd', "you're", 'looking', 'good', 'today']
16+
// example 4: str_word_count('hey', 2);
17+
// returns 4: {0: 'hey'}
1618

1719
var len = str.length,
1820
cl = charlist && charlist.length,
@@ -102,4 +104,4 @@ function str_word_count(str, format, charlist) {
102104
}
103105

104106
throw 'You have supplied an incorrect format';
105-
}
107+
}

lib/phpjsutil.js

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -248,18 +248,9 @@ PhpjsUtil.prototype.test = function(params, cb) {
248248
var extracted = self.getRecursiveCode(params);
249249
codez = codez.concat(codez, extracted);
250250

251-
codez.unshift('' +
252-
'XMLHttpRequest = {};' +
253-
'window = {' +
254-
'document: {' +
255-
'lastModified: 1388954399,' +
256-
'getElementsByTagName: function(){return [];}' +
257-
'},' +
258-
'location: {' +
259-
'href: ""' +
260-
'}' +
261-
'};' +
262-
'');
251+
for (var global in self.globals) {
252+
codez.unshift(global + ' = ' + self.globals[global] + ';');
253+
}
263254

264255
// Load code
265256
// self. refers to own function

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "phpjs",
3-
"version": "1.0.2",
3+
"version": "1.1.2",
44
"description": "php.js offers community built php functions in javascript",
55
"homepage": "http://phpjs.org",
66
"author": "Kevin van Zonneveld <kevin@vanzonneveld.net>",

0 commit comments

Comments
 (0)