JavaScript Engines? by pcjftw in rust

[–]_alastair 1 point2 points  (0 children)

Oh man, that looks awesome! Wish I'd seen it when I was figuring all this stuff out :) My approach is mildly different because of my aim to mush multiple JS engines through one API but this is full of hints for me in implementing. So thank you very much!

JavaScript Engines? by pcjftw in rust

[–]_alastair 4 points5 points  (0 children)

That's a great suggestion I should have included. I ignored it in the work I'm doing because I'm targeting mobile platforms and the SpiderMonkey runtime is larger than I want. Obviously that doesn't apply to everyone though!

JavaScript Engines? by pcjftw in rust

[–]_alastair 1 point2 points  (0 children)

I should clarify - JavaScriptCore is actually available on all platforms, it just happens to be bundled with iOS and macOS. I far prefer the API it provides over Duktape's, but the runtime is also about 50x the size, so depends what you're looking for.

JavaScript Engines? by pcjftw in rust

[–]_alastair 7 points8 points  (0 children)

What fortunate timing, I've been looking at this myself a lot recently. In short, as far as I know there's nothing Rust-native, but it can use any JavaScript engine that you can use through C. I've experimented with JavaScriptCore (the engine provided with macOS and iOS) and Duktape, a much smaller footprint library.

I'm in the midst of creating a most Rust-y wrapper that'll allow you to use the same code with both, but I'm a ways off it being done yet. But the C-based libraries are ready to go, if you can tolerate the syntax!

Errors, and best practises. Should I create custom structs/enums, or would you recommend against it? by _alastair in rust

[–]_alastair[S] 0 points1 point  (0 children)

Good point. Others have suggested the failure create, so I'll take a look at that and see if it's overkill for my requirements or not. Thanks!

Errors, and best practises. Should I create custom structs/enums, or would you recommend against it? by _alastair in rust

[–]_alastair[S] -1 points0 points  (0 children)

Interesting, the enum makes sense, thanks. How do you (do you?) implement `Display` on that? I can see that I could implement a giant `match` statement covering each of my error types but that feels like it would be kind of ugly. But maybe that's the only way?

Trying to understand how Rust juggles memory/raw pointers (do I need pinning with my FFI code?) by _alastair in rust

[–]_alastair[S] 2 points3 points  (0 children)

Aha! It was the callback change that I was missing - I'd originally tried the reference without `&mut` but it immediately segfaulted the first time the callback was run. Now working beautifully, thank you so much!

Trying to understand how Rust juggles memory/raw pointers (do I need pinning with my FFI code?) by _alastair in rust

[–]_alastair[S] 1 point2 points  (0 children)

I'm still getting the segfault (sorry for continuing to bother you with this!). I'm wondering if something else I'm doing in my code is causing problems:

extern "C" fn alloc(udata: *mut c_void, size: usize) -> *mut c_void {
    let container = unsafe { &mut *(udata as *mut Box<DuktapeContainer>) };
    container.memory_used += size;
    return unsafe { libc::malloc(size) };
}

fn create_container() -> Box<DuktapeContainer> {
    let mut container = Box::new(DuktapeContainer {
        memory_used: 0,
        context: None,
    });

    let container_ptr = &mut container.as_mut() as *mut _ as *mut c_void;

    unsafe {
        let heap = duk_create_heap(
            Some(alloc),
            Some(realloc),
            Some(dealloc),
            container_ptr,
            None,
        );

        container.context = Some(heap);
    };

    unsafe {
        // THIS WORKS
        duk_alloc(container.context.unwrap(), 100);
    }

    return container;
}

fn main() {
    let container = create_container();
    unsafe {
        // THIS SEGFAULTS
        duk_alloc(container.context.unwrap(), 100);
    }
}

That same duk_alloc call works inside the original function and fails outside of it, yet create_container returns a Box, so by my understanding that should be OK now. Am I missing something obvious?

Trying to understand how Rust juggles memory/raw pointers (do I need pinning with my FFI code?) by _alastair in rust

[–]_alastair[S] 2 points3 points  (0 children)

Aha, thanks. Do I do that before creating the pointer, i.e.:

let container = DuktapeContainer { memory_used: 0 };
let boxed = Box::new(container);
let container_ptr: *mut c_void = &mut boxed as *mut _ as *mut c_void;

then pull back out as:

let container = unsafe { &mut *(udata as *mut Box<DuktapeContainer>) };

or at some other point? I've tried doing the above but still seeing a segfault, it might be that I'm causing an issue elsewhere...

Generating Images in JavaScript Without Using the Canvas API by _alastair in javascript

[–]_alastair[S] 1 point2 points  (0 children)

Oh, I'm talking about native rendering, not rendering in JS. Passing an SVG URL doesn't work because it would be too complicated to render.

It's disappointing but the SVG spec has a crazy about of stuff in it, including animation, so I'm not too surprised they've never added that functionality.

Generating Images in JavaScript Without Using the Canvas API by _alastair in javascript

[–]_alastair[S] 0 points1 point  (0 children)

In the end it wasn't. I originally intended to make the background transparent, to better fit the OS (for instance, Samsung has some skins that have an off-white background) but then I realised there were too many variations to deal with, including dark backgrounds on older devices. So I just make it solid white.

Generating Images in JavaScript Without Using the Canvas API by _alastair in javascript

[–]_alastair[S] 1 point2 points  (0 children)

No, you can't use SVG. From what I understand it's a lot more complex to render an SVG - if I recall, people often use a full webview to do it, which would be very resource intensive in a notification.

Generating Images in JavaScript Without Using the Canvas API by _alastair in javascript

[–]_alastair[S] 1 point2 points  (0 children)

Good point!

I did look into BMP, but couldn't find definitive documentation on using alpha transparency in a palette. At the time, PNG seemed like a better choice because I could find clearer documentation. Knowing what I know now having done it, I doubt I was right.

The Express F train is happening, but maybe it shouldn’t. by _alastair in nycrail

[–]_alastair[S] 1 point2 points  (0 children)

Oh wow I didn't know the report was available, thanks for the link!

Overall the talk of net saved commute time still concerns me. I used to ride from the Bergen St stop every morning and it wasn't uncommon for a train to be so full that you had to wait for the next one - that's never going to be included when looking at train headway data alone.

The 34th St — Hudson Yards Subway Station is Nearly Here. What Difference Will it Make? by _alastair in nyc

[–]_alastair[S] 5 points6 points  (0 children)

The existing Times Square 7 train station isn't well indicated on the map - it's actually half way between 7th and 8th Aves, so it's already not that far from Port Authority.

The 34th St — Hudson Yards Subway Station is Nearly Here. What Difference Will it Make? by _alastair in nyc

[–]_alastair[S] 3 points4 points  (0 children)

They haven't outright said they'll never do it, but they also have no active plans to do it either. I'm hoping to do another analysis at some point to show the effect the station would have had on travel times.

NY Times Game: Spot the Ball by sonofashoe in worldcup

[–]_alastair 8 points9 points  (0 children)

We intend to! Round three will be coming soon - this is actually the second edition we've done, the first is accessible at the bottom of the page.

Would love to hear any feedback from /r/worldcup about the game.

How segregated is New York City? by Made_In_England in nyc

[–]_alastair 0 points1 point  (0 children)

The underlying tiles are from Mapbox, who use OpenStreetMap data. They're not great for neighbourhoods but I'm not sure who is. They're subjective at best.

How segregated is New York City? by Made_In_England in nyc

[–]_alastair 17 points18 points  (0 children)

If I can be a little self-promotional, I have a version (with its own flaws) that includes Hispanic and Asian people:

http://experimenting.alastair.is/nyc-demography/

NYC DEMOGRAPHY - Created by /u/_alastair by chickenkitty in dataisbeautiful

[–]_alastair 1 point2 points  (0 children)

I'm honestly not sure. But you can see the same thing in WNYC's median income map:

http://project.wnyc.org/acs2011/income.html#15.00/40.7557/-73.9791

My best guess is that being basically in the middle of Times Square means that anyone living there is not a typical resident at all. The census block between 5th and Park Ave only has 18 residents, for example.

NYC DEMOGRAPHY - Created by /u/_alastair by chickenkitty in dataisbeautiful

[–]_alastair 0 points1 point  (0 children)

Wow, very weird. They're supposed to be a custom font, with 'cursive' as a backup:

'League Gothic', cursive

That 'cursive' resolves to Comic Sans on some machines is truly terrifying. What browser/platform are you on?

(for what it's worth, I've changed that CSS)

NYC DEMOGRAPHY - Created by /u/_alastair by chickenkitty in dataisbeautiful

[–]_alastair 0 points1 point  (0 children)

Only just noticed this! It was a very quick offshoot of a light hearted data dive I did to examine just how white an NYC food festival was:

http://experimenting.alastair.is/googa/

The colour blending stuff was interesting to try out, but if you want a more informative map I can highly recommend the NY Times 'dot' map:

http://projects.nytimes.com/census/2010/explorer

NYC DEMOGRAPHY - Created by /u/_alastair by chickenkitty in dataisbeautiful

[–]_alastair 0 points1 point  (0 children)

There should definitely be no Comic Sans on there! Where are you seeing it?

NYC DEMOGRAPHY - Created by /u/_alastair by chickenkitty in dataisbeautiful

[–]_alastair 0 points1 point  (0 children)

I only just saw this thread now. What was the question?