Opened 10 years ago

Closed 10 years ago

Last modified 5 years ago

#2394 closed enhancement (fixed)

[PATCH] Add prototype functions to FixedVector return values from C++ to JS

Reported by: sanderd17 Owned by: sanderd17
Priority: Should Have Milestone: Alpha 16
Component: Core engine Keywords: patch
Cc: Patch:

Description

By addin a prototype to the return values of f.e. GetPosition() from cmpPosition, doing stuff with those return values becomes a lot easier.

The patch gives a possibility to do this (the patch is still very untested though).

Attachments (3)

vector_prototype.diff (9.4 KB ) - added by sanderd17 10 years ago.
vector_prototype.2.diff (9.3 KB ) - added by sanderd17 10 years ago.
vector_prototype.3.diff (9.3 KB ) - added by sanderd17 10 years ago.

Download all attachments as: .zip

Change History (13)

by sanderd17, 10 years ago

Attachment: vector_prototype.diff added

comment:1 by sanderd17, 10 years ago

Keywords: patch review added

comment:2 by wraitii, 10 years ago

I don't understand the description, could you explain more?

comment:3 by sanderd17, 10 years ago

Basically, these methods aren't nice: http://trac.wildfiregames.com/changeset?reponame=&new=14606%40ps%2Ftrunk%2Fbinaries%2Fdata%2Fmods%2Fpublic%2Fglobalscripts%2FMath.js&old=13515%40ps%2Ftrunk%2Fbinaries%2Fdata%2Fmods%2Fpublic%2Fglobalscripts%2FMath.js

They're not general enough (are they 2D or 3D? Isn't a cross() supposed to return a vector? ...) and making them general under the existing Math object will be a pain. Instead adding a prototype is better, but that means that the data coming from C++ should also have the prototype (else you'll have lots of useless object creations in JS just to get access to the functions).

comment:4 by sanderd17, 10 years ago

A new version, with some rather ugly caching, but the method is a lot faster. While the first version doubled the time needed to query a position, this version has barely no influence.

The non-neccesary JS code isn't included in the patch below, but I did include a little profile function. When running the Huge combat demo, it gives you comparable values.

Last edited 10 years ago by sanderd17 (previous) (diff)

by sanderd17, 10 years ago

Attachment: vector_prototype.2.diff added

by sanderd17, 10 years ago

Attachment: vector_prototype.3.diff added

comment:5 by sanderd17, 10 years ago

Yves said something about not being threadsafe, and using ScriptValRooted instead. I hope the above patch is what was meant.

Still no slowdown compared to the original code.

comment:6 by Yves, 10 years ago

The rooting looks OK now. The input about threadsafety was loud thinking more or less. It doesn't look worse than other code now, so I think it should be OK too. As you said it's still a bit ugly that a value clearly related to vectors is stored in the ScriptInterface. But from the perspective that the conversions are implemented as part of ScriptInterface, it makes sense. A general caching mechanism for the ScriptInterface could make sense if other conversion functions can use that too.

comment:7 by sanderd17, 10 years ago

Owner: set to sanderd17
Resolution: fixed
Status: newclosed

In 14645:

Add vector prototype to vector-like return values from C++ to JS. Fixes #2394.

comment:8 by sanderd17, 10 years ago

Keywords: review removed

comment:9 by Yves, 10 years ago

In 14705:

Fixes a crash introduced in r14645 by ensuring that the CScriptValRooted values are destroyed before calling JS_DestroyContext.
I've tested the performance on Combat Demo (Huge) again with the code from #2394.
It's very close but probably a little bit lower (hard to tell because it's so close).

Closes #2408
Refs #2394

comment:10 by elexis, 5 years ago

In 22487:

Remove Vector2D/Vector3D prototype workaround from EngineScriptConversions.

Fixes #5376, refs #2394.
Differential Revision: https://code.wildfiregames.com/D1991
Patch By: Krinkle
Comments By: Vladislav, wraitii

Note: See TracTickets for help on using tickets.