mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-29 21:12:00 +00:00
(PHP) fixes getting indirect table, also fixes getInt method on 32bit machine.
This commit is contained in:
@@ -399,8 +399,13 @@ class ByteBuffer
|
|||||||
$sign = $index + (ByteBuffer::isLittleEndian() ? 3 : 0);
|
$sign = $index + (ByteBuffer::isLittleEndian() ? 3 : 0);
|
||||||
$issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80;
|
$issigned = isset($this->_buffer[$sign]) && ord($this->_buffer[$sign]) & 0x80;
|
||||||
|
|
||||||
// 4294967296 = 1 << 32 = Maximum unsigned 32-bit int
|
if (PHP_INT_SIZE > 4) {
|
||||||
return $issigned ? $result - 4294967296 : $result;
|
// 4294967296 = 1 << 32 = Maximum unsigned 32-bit int
|
||||||
|
return $issigned ? $result - 4294967296 : $result;
|
||||||
|
} else {
|
||||||
|
// 32bit / Windows treated number as signed integer.
|
||||||
|
return $result;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -249,7 +249,13 @@ namespace php {
|
|||||||
NumToString(field.value.offset) +
|
NumToString(field.value.offset) +
|
||||||
");\n";
|
");\n";
|
||||||
code += Indent + Indent;
|
code += Indent + Indent;
|
||||||
code += "return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : ";
|
code += "return $o != 0 ? $obj->init(";
|
||||||
|
if (field.value.type.struct_def->fixed)
|
||||||
|
{
|
||||||
|
code += "$o + $this->bb_pos, $this->bb) : ";
|
||||||
|
} else {
|
||||||
|
code += "$this->__indirect($o + $this->bb_pos), $this->bb) : ";
|
||||||
|
}
|
||||||
code += GenDefaultValue(field.value) + ";\n";
|
code += GenDefaultValue(field.value) + ";\n";
|
||||||
code += Indent + "}\n\n";
|
code += Indent + "}\n\n";
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -188,7 +188,7 @@ class Monster extends Table
|
|||||||
{
|
{
|
||||||
$obj = new Monster();
|
$obj = new Monster();
|
||||||
$o = $this->__offset(28);
|
$o = $this->__offset(28);
|
||||||
return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
|
return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -214,7 +214,7 @@ class Monster extends Table
|
|||||||
{
|
{
|
||||||
$obj = new Stat();
|
$obj = new Stat();
|
||||||
$o = $this->__offset(32);
|
$o = $this->__offset(32);
|
||||||
return $o != 0 ? $obj->init($o + $this->bb_pos, $this->bb) : 0;
|
return $o != 0 ? $obj->init($this->__indirect($o + $this->bb_pos), $this->bb) : 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -1,2 +1,3 @@
|
|||||||
..\flatc.exe -c -j -n -g -b -p --php -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
..\flatc.exe -c -j -n -g -b -p --php -s --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
||||||
..\flatc.exe -b --schema monster_test.fbs
|
..\flatc.exe -b --schema monster_test.fbs
|
||||||
|
..\flatc.exe -b .\monster_test.fbs .\monsterdata_indirect.json
|
||||||
@@ -1,2 +1,3 @@
|
|||||||
../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
../flatc --cpp --java --csharp --go --binary --python --js --php --gen-mutable --no-includes monster_test.fbs monsterdata_test.json
|
||||||
../flatc --binary --schema monster_test.fbs
|
../flatc --binary --schema monster_test.fbs
|
||||||
|
../flatc --binary monster_test.fbs monsterdata_indirect.json
|
||||||
6
tests/monsterdata_indirect.json
Normal file
6
tests/monsterdata_indirect.json
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
{
|
||||||
|
"name": "Gob",
|
||||||
|
"enemy": {
|
||||||
|
"name": "Awk"
|
||||||
|
}
|
||||||
|
}
|
||||||
BIN
tests/monsterdata_indirect.mon
Normal file
BIN
tests/monsterdata_indirect.mon
Normal file
Binary file not shown.
@@ -74,6 +74,9 @@ function main()
|
|||||||
fuzzTest1($assert);
|
fuzzTest1($assert);
|
||||||
// testUnicode($assert);
|
// testUnicode($assert);
|
||||||
|
|
||||||
|
|
||||||
|
testIndirectBuffer($assert);
|
||||||
|
|
||||||
echo 'FlatBuffers php test: completed successfully' . PHP_EOL;
|
echo 'FlatBuffers php test: completed successfully' . PHP_EOL;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -587,7 +590,15 @@ function testByteBuffer(Assert $assert) {
|
|||||||
$assert->Equal(0x0D0C0B0A, $uut->readLittleEndian(0, 4, true));
|
$assert->Equal(0x0D0C0B0A, $uut->readLittleEndian(0, 4, true));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
function testIndirectBuffer(Assert $assert)
|
||||||
|
{
|
||||||
|
$js = json_decode(file_get_contents('monsterdata_indirect.json'), true);
|
||||||
|
$data = file_get_contents('monsterdata_indirect.mon');
|
||||||
|
$bb = Google\FlatBuffers\ByteBuffer::wrap($data);
|
||||||
|
$mons = \MyGame\Example\Monster::getRootAsMonster($bb);
|
||||||
|
$assert->Equal($js["name"], $mons->getName());
|
||||||
|
$assert->Equal($js["enemy"]["name"], $mons->getEnemy()->getName());
|
||||||
|
}
|
||||||
class Assert {
|
class Assert {
|
||||||
public function ok($result, $message = "") {
|
public function ok($result, $message = "") {
|
||||||
if (!$result){
|
if (!$result){
|
||||||
|
|||||||
Reference in New Issue
Block a user