Skip to content

Commit

Permalink
Merge branch 'master' into liblzma_port
Browse files Browse the repository at this point in the history
  • Loading branch information
Milek7 authored Dec 14, 2020
2 parents 90dd4d4 + de12acd commit fbcf808
Show file tree
Hide file tree
Showing 57 changed files with 429 additions and 322 deletions.
2 changes: 1 addition & 1 deletion .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "tests/third_party/posixtestsuite"]
path = tests/third_party/posixtestsuite
url = https://github.com/juj/posixtestsuite
url = https://github.com/emscripten-core/posixtestsuite
3 changes: 2 additions & 1 deletion AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -529,4 +529,5 @@ a license to everyone to use it as detailed in LICENSE.)
* Max Brunsfeld <maxbrunsfeld@gmail.com>
* Basil Fierz <basil.fierz@hotmail.com>
* Rod Hyde <rod@badlydrawngames.com>
* Miłosz Rachwał <me@milek7.pl>
* Aleksey Kliger <aleksey@lambdageek.org> (copyright owned by Microsoft, Inc.)
* Miłosz Rachwał <me@milek7.pl>
2 changes: 1 addition & 1 deletion emcc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1605,7 +1605,7 @@ def filter_out_duplicate_dynamic_libs(inputs):
building.user_requested_exports += ['exit']

if shared.Settings.PROXY_TO_PTHREAD:
shared.Settings.EXPORTED_FUNCTIONS += ['_proxy_main']
shared.Settings.EXPORTED_FUNCTIONS += ['_emscripten_proxy_main']

# pthread stack setup and other necessary utilities
def include_and_export(name):
Expand Down
19 changes: 12 additions & 7 deletions src/library.js
Original file line number Diff line number Diff line change
Expand Up @@ -2294,16 +2294,22 @@ LibraryManager.library = {
return { family: family, addr: addr, port: port };
},
_write_sockaddr__deps: ['$Sockets', '_inet_pton4_raw', '_inet_pton6_raw'],
_write_sockaddr: function (sa, family, addr, port) {
_write_sockaddr: function (sa, family, addr, port, addrlen) {
switch (family) {
case {{{ cDefine('AF_INET') }}}:
addr = __inet_pton4_raw(addr);
if (addrlen) {
{{{ makeSetValue('addrlen', 0, C_STRUCTS.sockaddr_in.__size__, 'i32') }}};
}
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_family, 'family', 'i16') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_addr.s_addr, 'addr', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in.sin_port, '_htons(port)', 'i16') }}};
break;
case {{{ cDefine('AF_INET6') }}}:
addr = __inet_pton6_raw(addr);
if (addrlen) {
{{{ makeSetValue('addrlen', 0, C_STRUCTS.sockaddr_in6.__size__, 'i32') }}};
}
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_family, 'family', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_addr.__in6_union.__s6_addr+0, 'addr[0]', 'i32') }}};
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_addr.__in6_union.__s6_addr+4, 'addr[1]', 'i32') }}};
Expand All @@ -2314,10 +2320,9 @@ LibraryManager.library = {
{{{ makeSetValue('sa', C_STRUCTS.sockaddr_in6.sin6_scope_id, '0', 'i32') }}};
break;
default:
return { errno: {{{ cDefine('EAFNOSUPPORT') }}} };
return {{{ cDefine('EAFNOSUPPORT') }}};
}
// kind of lame, but let's match _read_sockaddr's interface
return {};
return 0;
},

// We can't actually resolve hostnames in the browser, so instead
Expand Down Expand Up @@ -2447,7 +2452,7 @@ LibraryManager.library = {

function allocaddrinfo(family, type, proto, canon, addr, port) {
var sa, salen, ai;
var res;
var errno;

salen = family === {{{ cDefine('AF_INET6') }}} ?
{{{ C_STRUCTS.sockaddr_in6.__size__ }}} :
Expand All @@ -2456,8 +2461,8 @@ LibraryManager.library = {
__inet_ntop6_raw(addr) :
__inet_ntop4_raw(addr);
sa = _malloc(salen);
res = __write_sockaddr(sa, family, addr, port);
assert(!res.errno);
errno = __write_sockaddr(sa, family, addr, port);
assert(!errno);

ai = _malloc({{{ C_STRUCTS.addrinfo.__size__ }}});
{{{ makeSetValue('ai', C_STRUCTS.addrinfo.ai_family, 'family', 'i32') }}};
Expand Down
23 changes: 4 additions & 19 deletions src/library_pthread.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ var LibraryPThread = {
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tsd }}} ) >> 2, tlsMemory); // Init thread-local-storage memory array.
Atomics.store(HEAPU32, (tb + {{{ C_STRUCTS.pthread.tid }}} ) >> 2, tb); // Main thread ID.

PThread.initShared();

#if PTHREADS_PROFILING
PThread.createProfilerBlock(tb);
PThread.setThreadName(tb, "Browser main thread");
Expand All @@ -81,7 +79,6 @@ var LibraryPThread = {
#endif
},
initWorker: function() {
PThread.initShared();
#if MODULARIZE
// The promise resolve function typically gets called as part of the execution
// of the Module `run`. The workers/pthreads don't execute `run` here, they
Expand All @@ -97,11 +94,6 @@ var LibraryPThread = {
PThread['setThreadStatus'] = PThread.setThreadStatus;
PThread['threadCancel'] = PThread.threadCancel;
PThread['threadExit'] = PThread.threadExit;
#endif
},
initShared: function() {
#if ASSERTIONS
assert(__emscripten_main_thread_futex > 0);
#endif
},
// Maps pthread_t to pthread info objects
Expand Down Expand Up @@ -1318,21 +1310,15 @@ var LibraryPThread = {
#endif
},

emscripten_set_thread_name_js: function(threadId, name) {
#if PTHREADS_PROFILING
PThread.setThreadName(threadId, UTF8ToString(name));
#endif
},

// The profiler setters are defined twice, here in asm.js so that they can be #if'ed out
// without having to pay the impact of a FFI transition for a no-op in non-profiling builds.
emscripten_conditional_set_current_thread_status__asm: true,
emscripten_conditional_set_current_thread_status__sig: 'vii',
emscripten_conditional_set_current_thread_status__deps: ['emscripten_conditional_set_current_thread_status_js'],
emscripten_conditional_set_current_thread_status: function(expectedStatus, newStatus) {
#if PTHREADS_PROFILING
expectedStatus = expectedStatus|0;
newStatus = newStatus|0;
#if PTHREADS_PROFILING
_emscripten_conditional_set_current_thread_status_js(expectedStatus|0, newStatus|0);
#endif
},
Expand All @@ -1341,20 +1327,19 @@ var LibraryPThread = {
emscripten_set_current_thread_status__sig: 'vi',
emscripten_set_current_thread_status__deps: ['emscripten_set_current_thread_status_js'],
emscripten_set_current_thread_status: function(newStatus) {
newStatus = newStatus|0;
#if PTHREADS_PROFILING
newStatus = newStatus|0;
_emscripten_set_current_thread_status_js(newStatus|0);
#endif
},

emscripten_set_thread_name__asm: true,
emscripten_set_thread_name__sig: 'vii',
emscripten_set_thread_name__deps: ['emscripten_set_thread_name_js'],
emscripten_set_thread_name: function(threadId, name) {
#if PTHREADS_PROFILING
threadId = threadId|0;
name = name|0;
#if PTHREADS_PROFILING
_emscripten_set_thread_name_js(threadId|0, name|0);
PThread.setThreadName(threadId, UTF8ToString(name));
#endif
},

Expand Down
22 changes: 11 additions & 11 deletions src/library_syscall.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ var SyscallsLibrary = {
},
__sys_open: function(path, flags, varargs) {
var pathname = SYSCALLS.getStr(path);
var mode = SYSCALLS.get();
var mode = varargs ? SYSCALLS.get() : 0;
var stream = FS.open(pathname, flags, mode);
return stream.fd;
},
Expand Down Expand Up @@ -583,19 +583,19 @@ var SyscallsLibrary = {
var sock = getSocketFromFD(), addr = SYSCALLS.get(), addrlen = SYSCALLS.get();
var newsock = sock.sock_ops.accept(sock);
if (addr) {
var res = __write_sockaddr(addr, newsock.family, DNS.lookup_name(newsock.daddr), newsock.dport);
var errno = __write_sockaddr(addr, newsock.family, DNS.lookup_name(newsock.daddr), newsock.dport, addrlen);
#if ASSERTIONS
assert(!res.errno);
assert(!errno);
#endif
}
return newsock.stream.fd;
}
case 6: { // getsockname
var sock = getSocketFromFD(), addr = SYSCALLS.get(), addrlen = SYSCALLS.get();
// TODO: sock.saddr should never be undefined, see TODO in websocket_sock_ops.getname
var res = __write_sockaddr(addr, sock.family, DNS.lookup_name(sock.saddr || '0.0.0.0'), sock.sport);
var errno = __write_sockaddr(addr, sock.family, DNS.lookup_name(sock.saddr || '0.0.0.0'), sock.sport, addrlen);
#if ASSERTIONS
assert(!res.errno);
assert(!errno);
#endif
return 0;
}
Expand All @@ -604,9 +604,9 @@ var SyscallsLibrary = {
if (!sock.daddr) {
return -{{{ cDefine('ENOTCONN') }}}; // The socket is not connected.
}
var res = __write_sockaddr(addr, sock.family, DNS.lookup_name(sock.daddr), sock.dport);
var errno = __write_sockaddr(addr, sock.family, DNS.lookup_name(sock.daddr), sock.dport, addrlen);
#if ASSERTIONS
assert(!res.errno);
assert(!errno);
#endif
return 0;
}
Expand All @@ -625,9 +625,9 @@ var SyscallsLibrary = {
var msg = sock.sock_ops.recvmsg(sock, len);
if (!msg) return 0; // socket is closed
if (addr) {
var res = __write_sockaddr(addr, sock.family, DNS.lookup_name(msg.addr), msg.port);
var errno = __write_sockaddr(addr, sock.family, DNS.lookup_name(msg.addr), msg.port, addrlen);
#if ASSERTIONS
assert(!res.errno);
assert(!errno);
#endif
}
HEAPU8.set(msg.buffer, buf);
Expand Down Expand Up @@ -705,9 +705,9 @@ var SyscallsLibrary = {
// write the source address out
var name = {{{ makeGetValue('message', C_STRUCTS.msghdr.msg_name, '*') }}};
if (name) {
var res = __write_sockaddr(name, sock.family, DNS.lookup_name(msg.addr), msg.port);
var errno = __write_sockaddr(name, sock.family, DNS.lookup_name(msg.addr), msg.port);
#if ASSERTIONS
assert(!res.errno);
assert(!errno);
#endif
}
// write the buffer out to the scatter-gather arrays
Expand Down
41 changes: 31 additions & 10 deletions src/library_websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var LibraryWebSocket = {
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}

HEAPU16[readyState>>1] = socket.readyState;
{{{ makeSetValue('readyState', '0', 'socket.readyState', 'i16') }}};
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand Down Expand Up @@ -53,6 +53,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
if (!extensions) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
stringToUTF8(socket.extensions, extensions, extensionsLength);
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -67,6 +69,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
if (!extensionsLength) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
{{{ makeSetValue('extensionsLength', '0', 'lengthBytesUTF8(socket.extensions)+1', 'i32') }}};
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -81,6 +85,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
if (!protocol) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
stringToUTF8(socket.protocol, protocol, protocolLength);
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -95,6 +101,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
if (!protocolLength) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
{{{ makeSetValue('protocolLength', '0', 'lengthBytesUTF8(socket.protocol)+1', 'i32') }}};
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -109,6 +117,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
if (!url) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
stringToUTF8(socket.url, url, urlLength);
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -123,7 +133,8 @@ var LibraryWebSocket = {
#endif
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_TARGET') }}};
}
HEAPU32[urlLength>>2] = lengthBytesUTF8(socket.url);
if (!urlLength) return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
{{{ makeSetValue('urlLength', '0', 'lengthBytesUTF8(socket.url)+1', 'i32') }}};
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand Down Expand Up @@ -287,18 +298,21 @@ var LibraryWebSocket = {
return {{{ cDefine('EMSCRIPTEN_RESULT_INVALID_PARAM') }}};
}

var url = UTF8ToString(HEAP32[createAttributes>>2]);
var createAttrs = createAttributes>>2;
var url = UTF8ToString(HEAP32[createAttrs]);
var protocols = HEAP32[createAttrs+1];
// TODO: Add support for createOnMainThread==false; currently all WebSocket connections are created on the main thread.
// var createOnMainThread = HEAP32[createAttrs+2];

// TODO: protocols
// TODO: createOnMainThread
var socket = new WebSocket(url);
var socket = protocols ? new WebSocket(url, UTF8ToString(protocols).split(',')) : new WebSocket(url);
// We always marshal received WebSocket data back to Wasm, so enable receiving the data as arraybuffers for easy marshalling.
socket.binaryType = 'arraybuffer';
// TODO: While strictly not necessary, this ID would be good to be unique across all threads to avoid confusion.
var socketId = WS.sockets.length;
WS.sockets[socketId] = socket;

#if WEBSOCKET_DEBUG
console.error('emscripten_websocket_new(url='+url+'): created socket ID ' + socketId + ')');
console.error('emscripten_websocket_new(url='+url+', protocols=' + (protocols?UTF8ToString(protocols).split(','):'null') + '): created socket ID ' + socketId + ')');
#endif
return socketId;
},
Expand Down Expand Up @@ -375,8 +389,14 @@ var LibraryWebSocket = {
#if WEBSOCKET_DEBUG
console.error('emscripten_websocket_close(socketId='+socketId+',code='+code+',reason='+reasonStr+')');
#endif
if (!code) code = undefined;
socket.close(code, reasonStr);
// According to WebSocket specification, only close codes that are recognized have integer values
// 1000-4999, with 3000-3999 and 4000-4999 denoting user-specified close codes:
// https://developer.mozilla.org/en-US/docs/Web/API/CloseEvent#Status_codes
// Therefore be careful to call the .close() function with exact number and types of parameters.
// Coerce code==0 to undefined, since Wasm->JS call can only marshal integers, and 0 is not allowed.
if (reason) socket.close(code || undefined, UTF8ToString(reason));
else if (code) socket.close(code);
else socket.close();
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -396,7 +416,7 @@ var LibraryWebSocket = {
console.error('emscripten_websocket_delete(socketId='+socketId+')');
#endif
socket.onopen = socket.onerror = socket.onclose = socket.onmessage = null;
socket = null;
delete WS.sockets[socket];
return {{{ cDefine('EMSCRIPTEN_RESULT_SUCCESS') }}};
},

Expand All @@ -421,6 +441,7 @@ var LibraryWebSocket = {
_emscripten_websocket_delete(i);
}
}
WS.sockets = [];
}
}

Expand Down
12 changes: 6 additions & 6 deletions src/postamble.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,15 @@ function callMain(args) {
#else
var entryFunction = Module['__initialize'];
#endif
#else
#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which pthread_create()s a new thread
// that will call the user's real main() for the application.
var entryFunction = Module['_emscripten_proxy_main'];
#else
var entryFunction = Module['_main'];
#endif
#endif

#if MAIN_MODULE
// Main modules can't tell if they have main() at compile time, since it may
Expand Down Expand Up @@ -166,11 +172,6 @@ function callMain(args) {
abortWrapperDepth += 2;
#endif

#if PROXY_TO_PTHREAD
// User requested the PROXY_TO_PTHREAD option, so call a stub main which pthread_create()s a new thread
// that will call the user's real main() for the application.
var ret = Module['_proxy_main'](argc, argv);
#else
#if STANDALONE_WASM
entryFunction();
// _start (in crt1.c) will call exit() if main return non-zero. So we know
Expand All @@ -179,7 +180,6 @@ function callMain(args) {
#else
var ret = entryFunction(argc, argv);
#endif // STANDALONE_WASM
#endif // PROXY_TO_PTHREAD

#if BENCHMARK
Module.realPrint('main() took ' + (Date.now() - start) + ' milliseconds');
Expand Down
2 changes: 1 addition & 1 deletion src/postamble_minimal.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ function run() {
// User requested the PROXY_TO_PTHREAD option, so call a stub main which
// pthread_create()s a new thread that will call the user's real main() for
// the application.
var ret = _proxy_main();
var ret = _emscripten_proxy_main();
#else
var ret = _main();

Expand Down
Loading

0 comments on commit fbcf808

Please sign in to comment.