Giter Club home page Giter Club logo

Comments (4)

p0358 avatar p0358 commented on May 31, 2024 1

Here's what I did in my use-case:

let outside = /* JS array of strings here */;
let formatter = (function formatter(i) { return outside[i] || i; });
let formatterstr = formatter.toString().replace("outside", JSON.stringify(outside));

parser = parser.uint8(name, {
    formatter: new Function("x", "return (" + formatterstr + ")(x)"),
});

it would be equivalent of doing:

parser = parser.uint8(name, {
    formatter: item => outside[item] || item
});

(if you come here from search, you may also be interested in my comment in #75)

from binary-parser.

NuSkooler avatar NuSkooler commented on May 31, 2024

Very interested in something for this. I'm in the process of replacing the old binary module with this one (which rocks BTW!), but the lack of the ability to use my own functions easily makes some work very cumbersome.

from binary-parser.

Simran-B avatar Simran-B commented on May 31, 2024

Could own functions be injected perhaps? Something along new Parser( [range] )?

from binary-parser.

gnarea avatar gnarea commented on May 31, 2024

I was going to raise a related issue but I won't because I think its root cause is the same as this one: binary-parser generates JS code that gets evaled, so that has profound implications on what you can do in an assert or formatter.

In my case I wasn't using external values at all. My formatter was simply (value) => value.readUIntLE(0, 3). But because I'm using TypeScript, the generated code referenced some external values so I got some pretty unhelpful errors like cov_1ux0jwh81w is not defined.

I think there should be a big warning about this.

@p0358's workaround worked for me, although I really wish I didn't have to do that:

{
  formatter: new Function('buf', 'return buf.readUIntLE(0, 3)'),
}

from binary-parser.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.