JSON output support for float and float4

VERIFIED FIXED in Q2 12 - Cyril

Status

Tamarin
Virtual Machine
P3
normal
VERIFIED FIXED
7 years ago
7 years ago

People

(Reporter: Lars T Hansen, Assigned: Lars T Hansen)

Tracking

(Blocks: 1 bug)

unspecified
Q2 12 - Cyril

Details

(Whiteboard: has-patch)

Attachments

(1 attachment, 3 obsolete attachments)

(Assignee)

Description

7 years ago
JSON being JSON, float has to be formatted as a Number, and a float4 as an Array of Number.
(Assignee)

Comment 1

7 years ago
Created attachment 575893 [details] [diff] [review]
Some preliminary work
(Assignee)

Comment 2

7 years ago
Created attachment 576697 [details] [diff] [review]
Alternative solution

Formats a float4 as a property list (object) instead.  For reasons I don't understand yet this patch does not work properly when I call it thusly:

JSON.stringify(float4(1,2,3,4), null, "   ")

as it then runs the property values together on a line like this:

{4321
}

but what I had expected is this (for some property order):

{
   "x": 1,
   "y": 2,
   "z": 3,
   "w": 4
}

To be continued later - not high priority.
For the first release, I'd just make the toJSON serialization pure AS code in JSON.as, rather than mucking about in the JSONClass.cpp logic.
(Assignee)

Comment 4

7 years ago
(In reply to Felix S Klock II from comment #3)
> For the first release, I'd just make the toJSON serialization pure AS code
> in JSON.as, rather than mucking about in the JSONClass.cpp logic.

Aha.  Where would you put that in?

(Also remember we need to ifdef the code, even in AS3 - certainly we can do that with our configuration system, we're already doing it for the float class, but it still needs to be accounted for.)
(In reply to Lars T Hansen from comment #4)
> (In reply to Felix S Klock II from comment #3)
> > For the first release, I'd just make the toJSON serialization pure AS code
> > in JSON.as, rather than mucking about in the JSONClass.cpp logic.
> 
> Aha.  Where would you put that in?
> 
> (Also remember we need to ifdef the code, even in AS3 - certainly we can do
> that with our configuration system, we're already doing it for the float
> class, but it still needs to be accounted for.)

See for example the (purposefully trivial) toJSON methods in core/{Date,XML,ByteArray}.as

The Float4 one would be slightly more complicated, but not too much so; just assemble the array or object, and return it.
(Assignee)

Comment 6

7 years ago
I will look into that.

My concern is that a float4 value in the VM is not a ScriptObject* so I'm guessing additional protocol may be needed to handle that case (new tags, for sure).  However, if we are already handling toJSON methods on eg Number properly then that should be simple work.
(In reply to Lars T Hansen from comment #6)
> My concern is that a float4 value in the VM is not a ScriptObject* so I'm
> guessing additional protocol may be needed to handle that case (new tags,
> for sure).  However, if we are already handling toJSON methods on eg Number
> properly then that should be simple work.

Yeah I would d say we were going to have to do this anyway, assuming we want users to be able to override toJSON for Float4 the same way that they can override ByteArray.toJSON.

I assume that we want float values to just use the Number.toJSON, analogously to how I believe int and uint are handled.  But maybe this is a flawed assumption, since there are actually not int/uint values at runtime (just Numbers), while I assume there *are* float values at runtime, right?  So maybe there should be a Float.toJSON?
(Assignee)

Comment 8

7 years ago
For the record:

Discussed with Edwin and Felix whether to format a float4 as an array [x,y,z,w] or as a property list {"x":x, "y":y, "z":z, "w":z}.  General consensus is that the latter is more desirable.
(Assignee)

Comment 9

7 years ago
(In reply to Felix S Klock II from comment #7)
> (In reply to Lars T Hansen from comment #6)
> > My concern is that a float4 value in the VM is not a ScriptObject* so I'm
> > guessing additional protocol may be needed to handle that case (new tags,
> > for sure).  However, if we are already handling toJSON methods on eg Number
> > properly then that should be simple work.
> 
> Yeah I would d say we were going to have to do this anyway, assuming we want
> users to be able to override toJSON for Float4 the same way that they can
> override ByteArray.toJSON.

The apropriate analogous case for both float and float4 is Number, so we should try to keep the discussion to that.

> I assume that we want float values to just use the Number.toJSON,
> analogously to how I believe int and uint are handled.  But maybe this is a
> flawed assumption, since there are actually not int/uint values at runtime
> (just Numbers), while I assume there *are* float values at runtime, right? 
> So maybe there should be a Float.toJSON?

There are no int and uint values, but there are float and float4 values.  JSON stringification on float and float4 should behave as on Number, I think, despite the fact that the float4 case looks more complicated.  If the JSON case for Number can be overridden then the case for float and float4 should be overridable; if not, then not.
(Assignee)

Comment 10

7 years ago
Created attachment 580904 [details] [diff] [review]
AS3 parts

On the high level: a float is a Number and should be treated that way; a float4 is not a structure and should not be subject to structure walking.
(Assignee)

Comment 11

7 years ago
Created attachment 580917 [details] [diff] [review]
Proposed solution

Won't land until tr-float works with AVMFEATURE_FLOAT=0 and I can test that the code for those cases works as it should.

Felix, great if you have time for a quick look, but not a requirement.
Attachment #580917 - Flags: feedback?(fklockii)
(Assignee)

Updated

7 years ago
Attachment #575893 - Attachment is obsolete: true
(Assignee)

Updated

7 years ago
Attachment #576697 - Attachment is obsolete: true
(Assignee)

Updated

7 years ago
Attachment #580904 - Attachment is obsolete: true
(Assignee)

Updated

7 years ago
Assignee: nobody → lhansen
Whiteboard: has-patch
Comment on attachment 580917 [details] [diff] [review]
Proposed solution

I had forgotten the mapping of non-finite values to "null".  Ah, JSON...

Considering your statement from comment 10 that "a float4 is not a structure and should not be subject to structure walking": I interpret JO as doing structure-walking (e.g. it does the JOProp loop, passing the key/value pairs to the replacer callback function, if any; see JSONSerializer::StrFoundValue).  Do you disagree?  (I am not saying that this traversal is wrong, in fact I think its fine; I am just pointing out a potential discrepancy in your patch and your comment.)
Attachment #580917 - Flags: feedback?(fklockii) → feedback+
(Assignee)

Comment 13

7 years ago
(In reply to Felix S Klock II from comment #12)
> Comment on attachment 580917 [details] [diff] [review]
> Proposed solution
> 
> I had forgotten the mapping of non-finite values to "null".  Ah, JSON...
> 
> Considering your statement from comment 10 that "a float4 is not a structure
> and should not be subject to structure walking": I interpret JO as doing
> structure-walking (e.g. it does the JOProp loop, passing the key/value pairs
> to the replacer callback function, if any; see
> JSONSerializer::StrFoundValue).  Do you disagree?

No, I do not disagree.  The comment was wrong.
(In reply to Lars T Hansen from comment #9)
> If the
> JSON case for Number can be overridden then the case for float and float4
> should be overridable; if not, then not.

avmplus interactive shell
Type '?' for help

> JSON.stringify({x:43, y:44.4, z:new Number(45), w:uint(-46)})
{"x":43,"z":45,"w":4294967250,"y":44.4}
> Number.prototype.toJSON = function (k) { return this + 10; }
function Function() {}
> JSON.stringify({x:43, y:44.4, z:new Number(45), w:uint(-46)})
{"x":53,"z":55,"w":4294967260,"y":54.4}
> 

So yes, they should be overriddable.

(And we should have tests for this feature of the Number prototype, but I do not yet see evidence of any such tests in test/acceptance/ecma3/JSON/ )
(Assignee)

Comment 15

7 years ago
Core JSON code, a little different from the patch:

changeset:   7069:dab3767ce798
tag:         tip
user:        Lars T Hansen <lhansen@adobe.com>
date:        Tue Dec 13 13:25:47 2011 +0100
summary:     For 704071 - JSON output support for float and float4: core code, no test cases yet

What I have here appears to do rewriting properly: toJSON on float and float4 are invoked and their results obeyed.
(Assignee)

Comment 16

7 years ago
Test cases:

changeset:   7070:0d65da7a9abb
tag:         tip
user:        Lars T Hansen <lhansen@adobe.com>
date:        Tue Dec 13 14:13:53 2011 +0100
summary:     JSON on float/float4: test cases
Status: NEW → RESOLVED
Last Resolved: 7 years ago
Resolution: --- → FIXED

Updated

7 years ago
Status: RESOLVED → VERIFIED
You need to log in before you can comment on or make changes to this bug.