Skip to content

Commit 2b8d764

Browse files
authored
Merge branch 'main' into cw-audio-memory64
2 parents 5d8cee3 + 2e2ec08 commit 2b8d764

File tree

10 files changed

+72
-32
lines changed

10 files changed

+72
-32
lines changed

site/source/docs/porting/connecting_cpp_and_javascript/embind.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1071,7 +1071,7 @@ Out of the box, *embind* provides converters for many standard C++ types:
10711071
\*\*Requires BigInt support to be enabled with the `-sWASM_BIGINT` flag.
10721072

10731073
For convenience, *embind* provides factory functions to register
1074-
``std::vector<T>`` (:cpp:func:`register_vector`), ``std::map<K, V>``
1074+
``std::vector<T, class Allocator=std::allocator<T>>`` (:cpp:func:`register_vector`), ``std::map<K, V, class Compare=std::less<K>, class Allocator=std::allocator<std::pair<const K, V>>>``
10751075
(:cpp:func:`register_map`), and ``std::optional<T>`` (:cpp:func:`register_optional`) types:
10761076

10771077
.. code:: cpp

src/jsifier.mjs

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99

1010
import assert from 'node:assert';
1111
import * as fs from 'node:fs/promises';
12-
import * as path from 'node:path';
1312
import {
1413
ATEXITS,
1514
ATINITS,
@@ -146,23 +145,27 @@ function shouldPreprocess(fileName) {
146145
return content.startsWith('#preprocess\n') || content.startsWith('#preprocess\r\n');
147146
}
148147

149-
function getIncludeFile(fileName, alwaysPreprocess) {
150-
let result = `// include: ${fileName}\n`;
151-
const absFile = path.isAbsolute(fileName) ? fileName : localFile(fileName);
152-
const doPreprocess = alwaysPreprocess || shouldPreprocess(absFile);
148+
function getIncludeFile(fileName, alwaysPreprocess, shortName) {
149+
shortName ??= fileName;
150+
let result = `// include: ${shortName}\n`;
151+
const doPreprocess = alwaysPreprocess || shouldPreprocess(fileName);
153152
if (doPreprocess) {
154-
result += processMacros(preprocess(absFile), fileName);
153+
result += processMacros(preprocess(fileName), fileName);
155154
} else {
156-
result += readFile(absFile);
155+
result += readFile(fileName);
157156
}
158-
result += `// end include: ${fileName}\n`;
157+
result += `// end include: ${shortName}\n`;
159158
return result;
160159
}
161160

161+
function getSystemIncludeFile(fileName) {
162+
return getIncludeFile(localFile(fileName), /*alwaysPreprocess=*/ true, /*shortName=*/ fileName);
163+
}
164+
162165
function preJS() {
163166
let result = '';
164167
for (const fileName of PRE_JS_FILES) {
165-
result += getIncludeFile(fileName, /*alwaysPreprocess=*/ false);
168+
result += getIncludeFile(fileName);
166169
}
167170
return result;
168171
}
@@ -714,8 +717,12 @@ function(${args}) {
714717
libraryItems.push(JS);
715718
}
716719

717-
function includeFile(fileName, alwaysPreprocess = true) {
718-
writeOutput(getIncludeFile(fileName, alwaysPreprocess));
720+
function includeSystemFile(fileName) {
721+
writeOutput(getSystemIncludeFile(fileName));
722+
}
723+
724+
function includeFile(fileName) {
725+
writeOutput(getIncludeFile(fileName));
719726
}
720727

721728
function finalCombiner() {
@@ -741,10 +748,10 @@ function(${args}) {
741748
postSets.push(...orderedPostSets);
742749

743750
const shellFile = MINIMAL_RUNTIME ? 'shell_minimal.js' : 'shell.js';
744-
includeFile(shellFile);
751+
includeSystemFile(shellFile);
745752

746753
const preFile = MINIMAL_RUNTIME ? 'preamble_minimal.js' : 'preamble.js';
747-
includeFile(preFile);
754+
includeSystemFile(preFile);
748755

749756
for (const item of libraryItems.concat(postSets)) {
750757
writeOutput(indentify(item || '', 2));
@@ -769,14 +776,14 @@ var proxiedFunctionTable = [
769776
writeOutput('// EMSCRIPTEN_END_FUNCS\n');
770777

771778
const postFile = MINIMAL_RUNTIME ? 'postamble_minimal.js' : 'postamble.js';
772-
includeFile(postFile);
779+
includeSystemFile(postFile);
773780

774781
for (const fileName of POST_JS_FILES) {
775-
includeFile(fileName, /*alwaysPreprocess=*/ false);
782+
includeFile(fileName);
776783
}
777784

778785
if (MODULARIZE) {
779-
includeFile('postamble_modularize.js');
786+
includeSystemFile('postamble_modularize.js');
780787
}
781788

782789
if (errorOccured()) {

src/lib/libglfw.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -836,7 +836,7 @@ var LibraryGLFW = {
836836
event.preventDefault();
837837

838838
#if FILESYSTEM
839-
var filenames = _malloc(event.dataTransfer.files.length*4);
839+
var filenames = _malloc(event.dataTransfer.files.length * {{{ POINTER_SIZE }}});
840840
var filenamesArray = [];
841841
var count = event.dataTransfer.files.length;
842842

@@ -858,7 +858,7 @@ var LibraryGLFW = {
858858
var data = e.target.result;
859859
FS.writeFile(path, new Uint8Array(data));
860860
if (++written === count) {
861-
{{{ makeDynCall('vpii', 'GLFW.active.dropFunc') }}}(GLFW.active.id, count, filenames);
861+
{{{ makeDynCall('vpip', 'GLFW.active.dropFunc') }}}(GLFW.active.id, count, filenames);
862862

863863
for (var i = 0; i < filenamesArray.length; ++i) {
864864
_free(filenamesArray[i]);
@@ -870,7 +870,7 @@ var LibraryGLFW = {
870870

871871
var filename = stringToNewUTF8(path);
872872
filenamesArray.push(filename);
873-
{{{ makeSetValue('filenames + i*4', '0', 'filename', POINTER_TYPE) }}};
873+
{{{ makeSetValue('filenames', `i*${POINTER_SIZE}` , 'filename', POINTER_TYPE) }}};
874874
}
875875

876876
for (var i = 0; i < count; ++i) {

system/include/emscripten/bind.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,9 +2073,9 @@ struct VectorAccess {
20732073

20742074
} // end namespace internal
20752075

2076-
template<typename T>
2077-
class_<std::vector<T>> register_vector(const char* name) {
2078-
typedef std::vector<T> VecType;
2076+
template<typename T, class Allocator=std::allocator<T>>
2077+
class_<std::vector<T, Allocator>> register_vector(const char* name) {
2078+
typedef std::vector<T, Allocator> VecType;
20792079
#if __cplusplus >= 201703L
20802080
register_optional<T>();
20812081
#endif
@@ -2151,9 +2151,10 @@ struct MapAccess {
21512151

21522152
} // end namespace internal
21532153

2154-
template<typename K, typename V>
2155-
class_<std::map<K, V>> register_map(const char* name) {
2156-
typedef std::map<K,V> MapType;
2154+
template<typename K, typename V, class Compare = std::less<K>,
2155+
class Allocator = std::allocator<std::pair<const K, V>>>
2156+
class_<std::map<K, V, Compare, Allocator>> register_map(const char* name) {
2157+
typedef std::map<K,V, Compare, Allocator> MapType;
21572158
#if __cplusplus >= 201703L
21582159
register_optional<V>();
21592160
#endif

test/embind/embind.test.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1185,6 +1185,26 @@ module({
11851185
});
11861186
});
11871187

1188+
BaseFixture.extend("map_with_greater_comparator", function() {
1189+
test("std::map with std::greater comparator", function() {
1190+
var map = cm.embind_test_get_int_string_greater_map();
1191+
assert.equal(2, map.size());
1192+
assert.equal("one", map.get(1));
1193+
assert.equal("two", map.get(2));
1194+
map.delete();
1195+
});
1196+
1197+
test("std::map with std::greater comparator keys are sorted in reverse", function() {
1198+
var map = cm.embind_test_get_int_string_greater_map();
1199+
var keys = map.keys();
1200+
assert.equal(2, keys.size());
1201+
assert.equal(2, keys.get(0));
1202+
assert.equal(1, keys.get(1));
1203+
keys.delete();
1204+
map.delete();
1205+
});
1206+
});
1207+
11881208
BaseFixture.extend("optional", function() {
11891209
if (!("embind_test_return_optional_int" in cm)) {
11901210
return;

test/embind/embind_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -885,6 +885,15 @@ std::map<std::string, int> embind_test_get_string_int_map() {
885885
return m;
886886
};
887887

888+
std::map<int, std::string, std::greater<int>> embind_test_get_int_string_greater_map() {
889+
std::map<int, std::string, std::greater<int>> m;
890+
891+
m[1] = "one";
892+
m[2] = "two";
893+
894+
return m;
895+
}
896+
888897
struct Vector {
889898
Vector() = delete;
890899

@@ -2373,6 +2382,9 @@ EMSCRIPTEN_BINDINGS(tests) {
23732382

23742383
register_map<std::string, int>("StringIntMap");
23752384
function("embind_test_get_string_int_map", embind_test_get_string_int_map);
2385+
2386+
register_map<int, std::string, std::greater<int>>("IntStringMapGreater");
2387+
function("embind_test_get_int_string_greater_map", embind_test_get_int_string_greater_map);
23762388

23772389
function("embind_test_getglobal", &embind_test_getglobal);
23782390

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
122093
1+
121986
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
232619
1+
232586

test/runner.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,8 +329,8 @@ def run_tests(options, suites):
329329
resultMessages = []
330330
num_failures = 0
331331

332-
print('Test suites:')
333-
print([s[0] for s in suites])
332+
if len(suites) > 1:
333+
print('Test suites:', [s[0] for s in suites])
334334
# Run the discovered tests
335335

336336
# We currently don't support xmlrunner on macOS M1 runner since

tools/link.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,8 +1826,8 @@ def get_full_import_name(name):
18261826
if settings.PTHREADS:
18271827
settings.REQUIRED_EXPORTS.append('_emscripten_tls_init')
18281828

1829-
settings.PRE_JS_FILES = [os.path.abspath(f) for f in options.pre_js]
1830-
settings.POST_JS_FILES = [os.path.abspath(f) for f in options.post_js]
1829+
settings.PRE_JS_FILES = options.pre_js
1830+
settings.POST_JS_FILES = options.post_js
18311831

18321832
settings.MINIFY_WHITESPACE = settings.OPT_LEVEL >= 2 and settings.DEBUG_LEVEL == 0 and not options.no_minify
18331833

0 commit comments

Comments
 (0)