question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to use the AnyPointer type?

See original GitHub issue

Hi,

I have a Request struct with the following schema:

struct Request {
	timestamp @0: UInt64;
	payload @1: AnyPointer;
}

The payload can either be a capnp.Data struct filled from a Node.js Buffer, or another user-defined struct (let’s say a Todo struct for example). How am I supposed to create a message with such payload?

I tried the following:

const msg = new capnp.Message();
const req = msg.initRoot(Request);
req.setTimestamp(capnp.Uint64.fromNumber(0));

// With a capnp.Data struct:
const payload = capnp.Struct.initStructAt(0, capnp.Data, req);
payload.copyBuffer(Buffer.from("coucou"));
req.setPayload(payload);
// or with a Todo struct:
const payload = capnp.Struct.initStrutAt(0, Todo, req);
payload.setText("Masterize capnp-ts");
payload.setDone(false);
res.setPayload(payload);

The payload is transmitted but the timestamp field seems corrupted when I set the payload. So, what is the correct way to fill this AnyPointer payload field?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jonathan-roycommented, Apr 11, 2019

I finally found a very hacky way to fill the capnp.Data struct. First, I created a DataBox struct with the following schema:

@0x915f3367709de788;
  
struct DataBox {
        value @0 :Data;
}

Then I use it like this:

const msg = new capnp.Message();
const dataBox = msg.initRoot(DataBox);
dataBox.initValue(yourReadyData.length);
const payload = dataBox.getValue();
payload.copyBuffer(yourReadyData);
req.setPayload(payload);

It is obviously not ideal so I won’t close this issue, but it seems to work.

0reactions
ndisidorecommented, May 16, 2022

Sorry for the necro thread, but this still seems to be an issue: this could definitely be user error, but as far as I can tell a few data types (Data & AnyPointer are the ones tested) are near impossible to use because copyBuffer has some fundamental issues in that it assumes your destination buffer is (at least) the right size.

In my setup, I’m working with a protobuf struct like

struct MyStruct {
  value @0 :Data;
}

Accompanying JS code looks like

const prepopulatedBuffer = new TextEncoder().encode('test')

const msg = new capnp.Message()
const myStruct = msg.initRoot(MyStruct)
const data = new capnp.Message().initRoot(capnp.Data)
...

I’d like to copy an externally populated Uint8Array prepopulatedBuffer to data. However, passing both the raw Uint8Array and the Uint8Array as an ArrayBuffer (via prepopulatedBuffer.buffer) don’t accomplish this because copyBuffer truncates based on the destination size (which I haven’t found a way to not be 0 yet) and results in the same behavior @jonathan-roy was seeing.

Read more comments on GitHub >

github_iconTop Results From Across the Web

any-pointer - CSS: Cascading Style Sheets - MDN Web Docs
The any-pointer CSS media feature tests whether the user has any pointing device (such as a mouse), and if so, how accurate it...
Read more >
Pointer and ANY Pointer - 253126 - Industry Support Siemens
In the helps file, i just know about pointer contains 6 bytes and ANY contains 10 bytes. I need to take the DB...
Read more >
Siemens S7 How to use ANY Pointer in Siemens step-7
In this video we have explained ANY pointer in simple way with an example.SFC 20 used with any pointer with dynamic Parameterization.
Read more >
C: Pointers to any type? - Stack Overflow
Yes, you can use a void* to point to anything, and then cast it to the proper type when needed (that's how malloc...
Read more >
Pointers and Pointer Types (Delphi) - RAD Studio
Any data type that requires large, dynamically allocated blocks of memory uses pointers. Long-string variables, for instance, are implicitly pointers, as are ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found