Skip to content

Commit c6c304a

Browse files
RReverserkripken
andauthored
Cleanup val.h docs (#15212)
Co-authored-by: Alon Zakai <[email protected]>
1 parent 1b82784 commit c6c304a

File tree

1 file changed

+49
-106
lines changed

1 file changed

+49
-106
lines changed

site/source/docs/api_reference/val.h.rst

Lines changed: 49 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
.. _val-h:
22

3-
================================
4-
val.h (under-construction)
5-
================================
6-
7-
.. COMMENT (Not rendered) : This created from val.h header file on 10 Aug 2014-03
3+
=====
4+
val.h
5+
=====
86

97
The *Embind* C++ class :cpp:class:`emscripten::val` (defined in `val.h <https://github.com/emscripten-core/emscripten/blob/main/system/include/emscripten/val.h>`_) is used to *transliterate* JavaScript code to C++.
108

@@ -31,7 +29,7 @@ Guide material for this class can be found in :ref:`embind-val-guide`.
3129
.. code:: cpp
3230
3331
val xhr = val::global("XMLHttpRequest").new_();
34-
xhr.call("open", std::string("GET"), std::string("http://url"));
32+
xhr.call<void>("open", std::string("GET"), std::string("http://url"));
3533
3634
You can test whether the ``open`` method call was successful using :cpp:func:`~emscripten::val::operator[]` to read an object property, then :cpp:func:`~emscripten::val::as` to coerce the type:
3735

@@ -48,7 +46,16 @@ Guide material for this class can be found in :ref:`embind-val-guide`.
4846
}
4947
5048
See :ref:`embind-val-guide` for other examples.
49+
50+
51+
.. warning:: JavaScript values can't be shared across threads, so neither can ``val`` instances that bind them.
52+
53+
For example, if you want to cache some JavaScript global as a ``val``, you need to retrieve and bind separate instances of that global by its name in each thread.
54+
The easiest way to do this is with a ``thread_local`` declaration:
55+
56+
.. code:: cpp
5157
58+
thread_local const val Uint8Array = val::global("Uint8Array");
5259
5360
.. todo::
5461

@@ -74,193 +81,129 @@ Guide material for this class can be found in :ref:`embind-val-guide`.
7481

7582
Creates and returns a new ``Array``.
7683

77-
:returns: The new ``Array``.
78-
7984

8085
.. cpp:function:: static val object()
8186

8287
Creates and returns a new ``Object``.
8388

84-
:returns: The new ``Object``.
85-
8689

8790
.. cpp:function:: static val undefined()
8891

8992
Creates a ``val`` that represents ``undefined``.
9093

91-
:returns: The ``val`` that represents ``undefined``.
92-
9394

9495
.. cpp:function:: static val null()
9596

96-
Creates a ``val`` that represents ``null``. ``val::undefined()`` is the same, but for undefined.
97-
98-
:returns: A ``val`` that represents ``null``.
99-
100-
101-
.. cpp:function:: static val take_ownership(internal::EM_VAL e)
102-
103-
**HamishW**-Replace with description.
104-
105-
:returns: **HamishW**-Replace with description.
97+
Creates a ``val`` that represents ``null``.
10698

10799

108100
.. cpp:function:: static val global(const char* name)
109101

110-
Looks up a global symbol.
111-
112-
:param const char* name: **HamishW**-Replace with description.
113-
:returns: **HamishW**-Replace with description.
102+
Looks up a global value by the specified ``name``.
114103

115104

116105

117106
.. cpp:function:: static val module_property(const char* name)
118107

119-
Looks up a symbol on the emscripten Module object.
120-
121-
:param const char* name: **HamishW**-Replace with description.
122-
:returns: **HamishW**-Replace with description.
108+
Looks up a value by the provided ``name`` on the Emscripten Module object.
123109

124110

125111
.. cpp:function:: explicit val(T&& value)
126112

127113
Constructor.
128114

129-
A ``val`` can be constructed by explicit construction from any C++ type. For example, ``val(true)`` or ``val(std::string("foo"))``.
130-
131-
:param T&& value: Any C++ type.
132-
133-
134-
**HamishW** Don't know how following "floating statement works". Leaving here for discussion
135-
``val() = delete;``
115+
Creates a ``val`` by conversion from any Embind-compatible C++ type.
116+
For example, ``val(true)`` or ``val(std::string("foo"))``.
136117

137118

138119
.. cpp:function:: explicit val(const char* v)
139120

140-
**HamishW**-Replace with description.
141-
142-
:param const char* v: **HamishW**-Replace with description.
121+
Constructs a ``val`` instance from a string literal.
143122

144123

145124
.. cpp:function:: val(val&& v)
146-
147-
**HamishW**-Replace with description.
148-
149-
:param val&& v: **HamishW**-Replace with description.
125+
126+
Moves ownership of a value to a new ``val`` instance.
150127

151128

152129
.. cpp:function:: val(const val& v)
153-
154-
**HamishW**-Replace with description.
155-
156-
:param const val& v: **HamishW**-Replace with description.
130+
131+
Creates another reference to the same value behind the provided ``val`` instance.
157132

158133

159134
.. cpp:function:: ~val()
160135

161-
Destructor. **HamishW**-Replace with further description or delete comment.
136+
Removes the currently bound value by decreasing its refcount.
162137

163138

164139
.. cpp:function:: val& operator=(val&& v)
165-
166-
**HamishW**-Replace with description.
167-
168-
:param val&& v: **HamishW**-Replace with description.
169-
:returns: **HamishW**-Replace with description.
140+
141+
Removes a reference to the currently bound value and takes over the provided one.
170142

171143

172144
.. cpp:function:: val& operator=(const val& v)
173145

174-
**HamishW**-Replace with description.
175-
176-
:param val&& v: **HamishW**-Replace with description.
177-
:returns: **HamishW**-Replace with description.
146+
Removes a reference to the currently bound value and creates another reference to
147+
the value behind the provided ``val`` instance.
178148

179149

180150
.. cpp:function:: bool hasOwnProperty(const char* key) const
181151

182-
Test whether ... **HamishW**-Replace with description.
183-
184-
:param const char* key: **HamishW**-Replace with description.
185-
:returns: **HamishW**-Replace with description.
186-
187-
188-
.. cpp:function:: val new_()
189-
190-
prototype:
152+
Checks if the JavaScript object has own (non-inherited) property with the specified name.
191153

192-
::
193154

194-
template<typename... Args>
195-
val new_(Args&&... args) const
155+
.. cpp:function:: val new_(Args&&... args) const
196156

197-
**HamishW**-Replace with description.
198-
199-
:param Args&&... args: **HamishW**-Replace with description. Note that this is a templated value.
200-
:returns: **HamishW**-Replace with description.
157+
Assumes that current value is a constructor, and creates an instance of it.
158+
Equivalent to a JavaScript expression `new currentValue(...)`.
201159

202160

203161

204162
.. cpp:function:: val operator[](const T& key) const
205163

206-
**HamishW**-Replace with description.
207-
208-
:param const T& key: **HamishW**-Replace with description. Note that this is a templated value.
209-
:returns: **HamishW**-Replace with description.
164+
Get the specified (``key``) property of a JavaScript object.
210165

211166

212167
.. cpp:function:: void set(const K& key, const val& v)
213168

214-
Set the specified (``key``) property of a JavaScript object (accessed through a ``val``) with the value ``v``. **HamishW**-Replace with description.
215-
216-
:param const K& key: **HamishW**-Replace with description. Note that this is a templated value.
217-
:param const val& v: **HamishW**-Replace with description. Note that this is a templated value.
218-
219-
220-
.. cpp:function:: val operator()(Args&&... args)
169+
Set the specified (``key``) property of a JavaScript object (accessed through a ``val``) with the value ``v``.
221170

222-
**HamishW**-Replace with description.
223171

224-
:param Args&&... args: **HamishW**-Replace with description. Note that this is a templated value.
172+
.. cpp:function:: val operator()(Args&&... args) const
173+
174+
Assumes that current value is a function, and invokes it with provided arguments.
225175

226176

227177
.. cpp:function:: ReturnValue call(const char* name, Args&&... args) const
228178

229-
**HamishW**-Replace with description.
230-
231-
:param const char* name: **HamishW**-Replace with description.
232-
:param Args&&... args: **HamishW**-Replace with description. Note that this is a templated value.
179+
Invokes the specified method (``name``) on the current object with provided arguments.
233180

234181

235182
.. cpp:function:: T as() const
236183

237-
**HamishW**-Replace with description.
238-
239-
:returns: **HamishW**-Replace with description. Note that this is a templated value.
184+
Converts current value to the specified C++ type.
240185

241186

242187
.. cpp:function:: val typeof() const
243188

244-
**HamishW**-Replace with description.
245-
246-
:returns: **HamishW**-Replace with description.
189+
Returns the result of a JavaScript ``typeof`` operator invoked on the current value.
247190

248191

249192
.. cpp:function:: std::vector<T> vecFromJSArray(const val& v)
250193

251-
Copies a javascript array into a std::vector, checking the type of each element.
252-
For a more efficient but unsafe version working with numbers, see convertJSArrayToNumberVector.
194+
Copies a JavaScript array into a ``std::vector<T>``, converting each element via ``.as<T>()``.
195+
For a more efficient but unsafe version working with numbers, see ``convertJSArrayToNumberVector``.
253196

254-
:param val v: The javascript array to be copied
255-
:returns: A std::vector<T> made from the javascript array
197+
:param val v: The JavaScript array to be copied
198+
:returns: A ``std::vector<T>`` made from the javascript array
256199

257200
.. cpp:function:: std::vector<T> convertJSArrayToNumberVector(const val& v)
258201

259-
Converts a javascript object into a std::vector<T> efficiently, as if using the javascript `Number()` function on each element.
260-
This is way more efficient than vecFromJSArray on any array with more than 2 values, but is less safe.
261-
No type checking is done, so any invalid array entry will silently be replaced by a NaN value (or 0 for interger types).
202+
Converts a JavaScript array into a ``std::vector<T>`` efficiently, as if using the javascript `Number()` function on each element.
203+
This is way more efficient than ``vecFromJSArray`` on any array with more than 2 values, but is not suitable for arrays of non-numeric values.
204+
No type checking is done, so any invalid array entry will silently be replaced by a NaN value (or 0 for integer types).
262205

263-
:param val v: The javascript (typed) array to be copied
206+
:param val v: The JavaScript (typed) array to be copied
264207
:returns: A std::vector<T> made from the javascript array
265208

266209

@@ -270,7 +213,7 @@ Guide material for this class can be found in :ref:`embind-val-guide`.
270213

271214
:returns: The fulfilled value.
272215

273-
This method requires :ref:`Asyncify` to be enabled.
216+
.. note:: This method requires :ref:`Asyncify` to be enabled.
274217

275218

276219
.. cpp:type: EMSCRIPTEN_SYMBOL(name)

0 commit comments

Comments
 (0)