Skip to content

Commit ef58bab

Browse files
committed
buffer is first argument to 419 udp write #1487
1 parent 22abd8f commit ef58bab

File tree

15 files changed

+46
-37
lines changed

15 files changed

+46
-37
lines changed

examples/io/udp/dns/dns.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ class Resolver {
9191

9292
if (request.id) {
9393
const servers = this.#servers;
94-
this.#socket.write(servers[request.state % servers.length], 53, packet.build());
94+
this.#socket.write(packet.build(), servers[request.state % servers.length], 53);
9595
}
9696
else
97-
this.#socket.write("224.0.0.251", 5353, packet.build());
97+
this.#socket.write(packet.build(), "224.0.0.251", 5353);
9898
}
9999
catch {
100100
if (!this.#timer) {

examples/io/udp/dnssd-discover/main.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ listener.add(multicastAddress);
3535
// send a single probe
3636
const probe = new Serializer({query: true, opcode: DNS.OPCODE.QUERY, authoritative: true});
3737
probe.add(DNS.SECTION.QUESTION, "_services._dns-sd._udp.local", DNS.RR.PTR, DNS.CLASS.IN);
38-
listener.write(multicastAddress, port, probe.build());
38+
listener.write(probe.build(), multicastAddress, port);
3939

4040

4141
function dumpPacket(packet, address)

examples/io/udp/sntp/sntp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ class SNTP {
6363
function request(address) {
6464
const packet = new Uint8Array(48);
6565
packet[0] = (4 << 3) | (3 << 0); // version 4, mode 3 (client)
66-
this.write(address, 123, packet);
66+
this.write(packet, address, 123);
6767
}
6868

6969
export default SNTP;

modules/io/socket/lin/udp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Moddable Tech, Inc.
2+
* Copyright (c) 2022-2025 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -225,12 +225,14 @@ void xs_udp_write(xsMachine *the)
225225
return;
226226
}
227227

228+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
229+
228230
dest_addr.sin_family = AF_INET;
229-
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(0)));
230-
port = xsmcToInteger(xsArg(1));
231+
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(1)));
232+
port = xsmcToInteger(xsArg(2));
231233
dest_addr.sin_port = htons(port);
232234

233-
xsmcGetBufferReadable(xsArg(2), &buffer, &byteLength);
235+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
234236
int result = sendto(udp->skt, buffer, byteLength, 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr));
235237
if (result < 0)
236238
xsUnknownError("sendto failed");

modules/io/socket/lwip/udp.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,16 +193,19 @@ void xs_udp_read(xsMachine *the)
193193
void xs_udp_write(xsMachine *the)
194194
{
195195
UDP udp = xsmcGetHostDataValidate(xsThis, (void *)&xsUDPHooks);
196-
uint16_t port = xsmcToInteger(xsArg(1));
196+
uint16_t port;
197197
ip_addr_t dst;
198198
xsUnsignedValue byteLength;
199199
err_t err;
200200
void *buffer;
201201

202-
if (!ipaddr_aton(xsmcToString(xsArg(0)), &dst))
202+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
203+
204+
if (!ipaddr_aton(xsmcToString(xsArg(1)), &dst))
203205
xsRangeError("invalid IP address");
206+
port = xsmcToInteger(xsArg(2));
204207

205-
xsmcGetBufferReadable(xsArg(2), &buffer, &byteLength);
208+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
206209
udp_sendto_safe(udp->skt, buffer, byteLength, &dst, port, &err);
207210
if (ERR_OK != err)
208211
xsUnknownError("UDP send failed");

modules/io/socket/mac/udp.m

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,12 +240,14 @@ void xs_udp_write(xsMachine *the)
240240
int port;
241241
struct sockaddr_in dest_addr = {0};
242242

243+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
244+
243245
dest_addr.sin_family = AF_INET;
244-
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(0)));
245-
port = xsmcToInteger(xsArg(1));
246+
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(1)));
247+
port = xsmcToInteger(xsArg(2));
246248
dest_addr.sin_port = htons(port);
247249

248-
xsmcGetBufferReadable(xsArg(2), &buffer, &byteLength);
250+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
249251
int result = sendto(udp->skt, buffer, byteLength, 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr));
250252
if (result < 0)
251253
xsUnknownError("sendto failed");

modules/io/socket/win/udp.c

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2022 Moddable Tech, Inc.
2+
* Copyright (c) 2022-2025 Moddable Tech, Inc.
33
*
44
* This file is part of the Moddable SDK Runtime.
55
*
@@ -218,12 +218,14 @@ void xs_udp_write(xsMachine *the)
218218
return;
219219
}
220220

221+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
222+
221223
dest_addr.sin_family = AF_INET;
222-
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(0)));
223-
port = xsmcToInteger(xsArg(1));
224+
dest_addr.sin_addr.s_addr = inet_addr(xsmcToString(xsArg(1)));
225+
port = xsmcToInteger(xsArg(2));
224226
dest_addr.sin_port = htons(port);
225227

226-
xsmcGetBufferReadable(xsArg(2), &buffer, &byteLength);
228+
xsmcGetBufferReadable(xsArg(0), &buffer, &byteLength);
227229
int result = sendto(udp->skt, buffer, byteLength, 0, (const struct sockaddr *)&dest_addr, sizeof(dest_addr));
228230
if (result < 0)
229231
xsUnknownError("sendto failed");

tests/modules/io/socket/udp/close.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@ u.close();
1919
u.close();
2020

2121
assert.throws(SyntaxError, () => u.read());
22-
assert.throws(SyntaxError, () => u.write(address, port, Uint8Array.of(1)));
22+
assert.throws(SyntaxError, () => u.write(Uint8Array.of(1), address, port));
2323

2424
$DONE();

tests/modules/io/socket/udp/constructor-port.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ let u = new UDP({
2828
});
2929

3030
let uu = new UDP({});
31-
uu.write(address, port, ArrayBuffer.fromString(testString));
31+
uu.write(ArrayBuffer.fromString(testString), address, port);
3232
uu.close();
3333

3434
assert.throws(TypeError, () => new UDP({port: Symbol()}));

tests/modules/io/socket/udp/invalid-receiver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function callWithInvalidReceivers(obj, functionName, ...args)
2929
}
3030

3131
callWithInvalidReceivers(u, "write", address, port, new ArrayBuffer(1));
32-
u.write(address, port, new ArrayBuffer(1));
32+
u.write(new ArrayBuffer(1), address, port);
3333

3434
assert(await readable >= 1, "expected at least 1 packet readable");
3535

tests/modules/io/socket/udp/no-callback.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ assert.sameValue(u.read(), undefined, "expect undefined before packet available"
1818

1919
const sntpPacket = new Uint8Array(48);
2020
sntpPacket[0] = (4 << 3) | (3 << 0); // version 4, mode 3 (client)
21-
u.write(address, 123, sntpPacket);
22-
u.write(address, 123, sntpPacket);
23-
u.write(address, 123, sntpPacket);
21+
u.write(sntpPacket, address, 123);
22+
u.write(sntpPacket, address, 123);
23+
u.write(sntpPacket, address, 123);
2424

2525
Timer.repeat(id => {
2626
try {

tests/modules/io/socket/udp/onReadable.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ const u = new UDP({
3333

3434
const sntpPacket = new Uint8Array(48);
3535
sntpPacket[0] = (4 << 3) | (3 << 0); // version 4, mode 3 (client)
36-
u.write(address, 123, sntpPacket);
37-
u.write(address, 123, sntpPacket);
38-
u.write(address, 123, sntpPacket);
36+
u.write(sntpPacket, address, 123);
37+
u.write(sntpPacket, address, 123);
38+
u.write(sntpPacket, address, 123);

tests/modules/io/socket/udp/read-outside-callback.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ assert.sameValue(u.read(), undefined, "expect undefined before packet available"
2626

2727
const sntpPacket = new Uint8Array(48);
2828
sntpPacket[0] = (4 << 3) | (3 << 0); // version 4, mode 3 (client)
29-
u.write(address, 123, sntpPacket);
30-
u.write(address, 123, sntpPacket);
31-
u.write(address, 123, sntpPacket);
29+
u.write(sntpPacket, address, 123);
30+
u.write(sntpPacket, address, 123);
31+
u.write(sntpPacket, address, 123);
3232

3333
assert(await p >= 1, "expected at least 1 packet");
3434

tests/modules/io/socket/udp/target.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,6 @@ assert.sameValue(u.target, target, "bad target after instantiation");
2727

2828
const sntpPacket = new Uint8Array(48);
2929
sntpPacket[0] = (4 << 3) | (3 << 0); // version 4, mode 3 (client)
30-
u.write(address, 123, sntpPacket);
31-
u.write(address, 123, sntpPacket);
32-
u.write(address, 123, sntpPacket);
30+
u.write(sntpPacket, address, 123);
31+
u.write(sntpPacket, address, 123);
32+
u.write(sntpPacket, address, 123);

tests/modules/io/socket/udp/write.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,11 @@ let u = new UDP({
2929

3030
let uu = new UDP({});
3131

32-
assert.throws(TypeError, () => uu.write(address, Symbol(), new ArrayBuffer(1)));
33-
assert.throws(TypeError, () => uu.write(Symbol(), port, new ArrayBuffer(1)));
34-
assert.throws(TypeError, () => uu.write(address, port, Symbol()));
32+
assert.throws(TypeError, () => uu.write(new ArrayBuffer(1), address, Symbol()));
33+
assert.throws(TypeError, () => uu.write(new ArrayBuffer(1), Symbol(), port));
34+
assert.throws(TypeError, () => uu.write(Symbol(), address, port));
3535
assert.throws(ReferenceError, () => uu.write(Uin8Array.of(1,2,3)));
3636

3737
const bytes = new Uint8Array(ArrayBuffer.fromString("__" + testString + "__"));
38-
uu.write(address, port, bytes.subarray(2, -2));
38+
uu.write(bytes.subarray(2, -2), address, port);
3939
uu.close();

0 commit comments

Comments
 (0)