asmblah / uniter

🎉 PHP in the browser and Node.js => Docs: https://phptojs.com/
https://asmblah.github.io/uniter/
Other
446 stars 42 forks source link

Passing a value back to JS from PHP that was passed from JS gets corrupted #40

Closed dave-irvine closed 7 years ago

dave-irvine commented 8 years ago
var php = require('uniter').createEngine('PHP');
php.getStdout().on('data', function (text) { console.log(text); });
php.getStderr().on('data', function (text) { console.error(text); });

var jsFunction = function() {
        return {
                "a": "b"
        };
}

var jsFunction2 = function(data) {
        console.log(data);
}

php.expose(jsFunction, 'jsFunction');
php.expose(jsFunction2, 'jsFunction2');
php.execute('<?php $x = $jsFunction(); $jsFunction2($x);');

{ objectValue: 
   { factory: 
      { nextObjectID: 4,
        callStack: [Object],
        globalNamespace: [Object],
        pausable: [Object] },
     callStack: { calls: [Object], stderr: [Object] },
     type: 'object',
     value: { a: 'b' },
     classObject: 
      { callStack: [Object],
        constants: undefined,
        constructorName: null,
        interfaceNames: [],
        InternalClass: [Function: JSObject],
        name: 'JSObject',
        namespaceScope: undefined,
        staticProperties: {},
        superClass: undefined,
        valueFactory: [Object] },
     id: 3,
     pointer: 0,
     properties: {} },
  pausable: 
   { transpiler: 
      { expressionTranspiler: [Object],
        statementTranspiler: [Object] } },
  valueFactory: 
   { nextObjectID: 4,
     callStack: { calls: [Object], stderr: [Object] },
     globalNamespace: 
      { callStack: [Object],
        children: {},
        classAutoloader: [Object],
        classes: [Object],
        constants: [Object],
        functionFactory: [Object],
        functions: [Object],
        name: '',
        namespaceFactory: [Object],
        parent: null,
        valueFactory: [Circular] },
     pausable: { transpiler: [Object] } } }
asmblah commented 8 years ago

Actually, this is the correct behaviour: this object is an instance of the PHPObject class (https://github.com/uniter/phpcore/blob/master/src/PHPObject.js) which deals with exposing PHP objects to JS-land.

asmblah commented 8 years ago

Thinking about it some more, in this specific case (where the returned value is an instance of JSObject, because it originated from JS-land) I think it might make sense to unwrap and return the native JS object again. In other cases though, a PHPObject that wraps the object would need to be returned to handle coercing method arguments from JS to PHP.

asmblah commented 7 years ago

Going through and clearing up some old issues...

This seems to be working as you expected in the latest PHPCore, probably since the fix in https://github.com/uniter/phpcore/commit/c845d0a599f37b68058b60fa34751ffa7586dac1.

Thanks!