Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in / Register
Toggle navigation
F
flatbuffers
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Packages
Packages
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
submodule
flatbuffers
Commits
617fedf3
Commit
617fedf3
authored
Feb 26, 2016
by
Wouter van Oortmerssen
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #3765 from Mischanix/js-typed-arrays
Javascript helper to get typed array views
parents
59caa536
9d92aeb1
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
48 additions
and
3 deletions
+48
-3
idl_gen_js.cpp
src/idl_gen_js.cpp
+13
-1
JavaScriptTest.js
tests/JavaScriptTest.js
+11
-2
monster_test_generated.js
tests/monster_test_generated.js
+24
-0
No files found.
src/idl_gen_js.cpp
View file @
617fedf3
...
...
@@ -509,12 +509,24 @@ static void GenStruct(const Parser &parser, StructDef &struct_def,
}
code
+=
"};
\n\n
"
;
// Emit
a length helper
// Emit
vector helpers
if
(
field
.
value
.
type
.
base_type
==
BASE_TYPE_VECTOR
)
{
// Emit a length helper
GenDocComment
(
code_ptr
,
"@returns {number}"
);
code
+=
object_name
+
".prototype."
+
MakeCamel
(
field
.
name
,
false
);
code
+=
"Length = function() {
\n
"
+
offset_prefix
;
code
+=
"this.bb.__vector_len(this.bb_pos + offset) : 0;
\n
};
\n\n
"
;
// For scalar types, emit a typed array helper
auto
vectorType
=
field
.
value
.
type
.
VectorType
();
if
(
IsScalar
(
vectorType
.
base_type
))
{
GenDocComment
(
code_ptr
,
"@returns {"
+
GenType
(
vectorType
)
+
"Array}"
);
code
+=
object_name
+
".prototype."
+
MakeCamel
(
field
.
name
,
false
);
code
+=
"Array = function() {
\n
"
+
offset_prefix
;
code
+=
"new "
+
GenType
(
vectorType
)
+
"Array(this.bb.bytes().buffer, "
"this.bb.__vector(this.bb_pos + offset), "
"this.bb.__vector_len(this.bb_pos + offset)) : null;
\n
};
\n\n
"
;
}
}
}
...
...
tests/JavaScriptTest.js
View file @
617fedf3
...
...
@@ -106,6 +106,13 @@ function testBuffer(bb) {
}
assert
.
strictEqual
(
invsum
,
10
);
var
invsum2
=
0
;
var
invArr
=
monster
.
inventoryArray
();
for
(
var
i
=
0
;
i
<
invArr
.
length
;
i
++
)
{
invsum2
+=
invArr
[
i
];
}
assert
.
strictEqual
(
invsum2
,
10
);
var
test_0
=
monster
.
test4
(
0
);
var
test_1
=
monster
.
test4
(
1
);
assert
.
strictEqual
(
monster
.
test4Length
(),
2
);
...
...
@@ -169,7 +176,7 @@ function testUnicode() {
var
json
=
JSON
.
parse
(
fs
.
readFileSync
(
'unicode_test.json'
,
'utf8'
));
// Test reading
var
bb
=
new
flatbuffers
.
ByteBuffer
(
new
Uint8Array
(
correct
));
function
testReadingUnicode
(
bb
)
{
var
monster
=
MyGame
.
Example
.
Monster
.
getRootAsMonster
(
bb
);
assert
.
strictEqual
(
monster
.
name
(),
json
.
name
);
assert
.
deepEqual
(
new
Buffer
(
monster
.
name
(
flatbuffers
.
Encoding
.
UTF8_BYTES
)),
new
Buffer
(
json
.
name
));
...
...
@@ -184,6 +191,8 @@ function testUnicode() {
assert
.
strictEqual
(
monster
.
testarrayofstring
(
i
),
string
);
assert
.
deepEqual
(
new
Buffer
(
monster
.
testarrayofstring
(
i
,
flatbuffers
.
Encoding
.
UTF8_BYTES
)),
new
Buffer
(
string
));
});
}
testReadingUnicode
(
new
flatbuffers
.
ByteBuffer
(
new
Uint8Array
(
correct
)));
// Test writing
var
fbb
=
new
flatbuffers
.
Builder
();
...
...
@@ -203,7 +212,7 @@ function testUnicode() {
MyGame
.
Example
.
Monster
.
addTestarrayoftables
(
fbb
,
testarrayoftablesOffset
);
MyGame
.
Example
.
Monster
.
addName
(
fbb
,
name
);
MyGame
.
Example
.
Monster
.
finishMonsterBuffer
(
fbb
,
MyGame
.
Example
.
Monster
.
endMonster
(
fbb
));
assert
.
deepEqual
(
new
Buffer
(
fbb
.
asUint8Array
()),
correct
);
testReadingUnicode
(
new
flatbuffers
.
ByteBuffer
(
fbb
.
asUint8Array
())
);
}
var
__imul
=
Math
.
imul
?
Math
.
imul
:
function
(
a
,
b
)
{
...
...
tests/monster_test_generated.js
View file @
617fedf3
...
...
@@ -447,6 +447,14 @@ MyGame.Example.Monster.prototype.inventoryLength = function() {
return
offset
?
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
)
:
0
;
};
/**
* @returns {Uint8Array}
*/
MyGame
.
Example
.
Monster
.
prototype
.
inventoryArray
=
function
()
{
var
offset
=
this
.
bb
.
__offset
(
this
.
bb_pos
,
14
);
return
offset
?
new
Uint8Array
(
this
.
bb
.
bytes
().
buffer
,
this
.
bb
.
__vector
(
this
.
bb_pos
+
offset
),
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
))
:
null
;
};
/**
* @returns {MyGame.Example.Color}
*/
...
...
@@ -555,6 +563,14 @@ MyGame.Example.Monster.prototype.testnestedflatbufferLength = function() {
return
offset
?
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
)
:
0
;
};
/**
* @returns {Uint8Array}
*/
MyGame
.
Example
.
Monster
.
prototype
.
testnestedflatbufferArray
=
function
()
{
var
offset
=
this
.
bb
.
__offset
(
this
.
bb_pos
,
30
);
return
offset
?
new
Uint8Array
(
this
.
bb
.
bytes
().
buffer
,
this
.
bb
.
__vector
(
this
.
bb_pos
+
offset
),
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
))
:
null
;
};
/**
* @param {MyGame.Example.Stat=} obj
* @returns {MyGame.Example.Stat}
...
...
@@ -653,6 +669,14 @@ MyGame.Example.Monster.prototype.testarrayofboolsLength = function() {
return
offset
?
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
)
:
0
;
};
/**
* @returns {Int8Array}
*/
MyGame
.
Example
.
Monster
.
prototype
.
testarrayofboolsArray
=
function
()
{
var
offset
=
this
.
bb
.
__offset
(
this
.
bb_pos
,
52
);
return
offset
?
new
Int8Array
(
this
.
bb
.
bytes
().
buffer
,
this
.
bb
.
__vector
(
this
.
bb_pos
+
offset
),
this
.
bb
.
__vector_len
(
this
.
bb_pos
+
offset
))
:
null
;
};
/**
* @param {flatbuffers.Builder} builder
*/
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment