Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Macro Voodoo #11

Merged
merged 2 commits into from
Dec 11, 2017
Merged

Macro Voodoo #11

merged 2 commits into from
Dec 11, 2017

Conversation

maciejhirsz
Copy link
Contributor

@maciejhirsz maciejhirsz commented Dec 1, 2017

This:

#[macro_use] extern crate neon;
#[macro_use] extern crate neon_serde;
#[macro_use] extern crate serde_derive;

#[derive(Deserialize)]
struct User {
    name: String,
    age: u16,
}

export! {
    fn say_hello(name: String) -> String {
        format!("Hello, {}!", name)
    }

    fn greet(user: User) -> String {
        format!("{} is {} years old", user.name, user.age)
    }
}

Works:

const native = require('../native');

describe('macro functions', () => {
    test('Hello, World!', () => {
        expect(native.say_hello('World')).toBe('Hello, World!');
        expect(native.say_hello('Alice')).toBe('Hello, Alice!');
        expect(native.say_hello('Bob')).toBe('Hello, Bob!');
    })

    test("Greet users", () => {
        expect(native.greet({ name: 'Bob', age: 32 })).toBe('Bob is 32 years old');
        expect(native.greet({ name: 'Alice', age: 27 })).toBe('Alice is 27 years old');
    })
});

Proof of concept more than anything, really.

@maciejhirsz
Copy link
Contributor Author

Updated the PR so that recursion works as expected:

export! {
    // ...

    fn fibonacci(n: i32) -> i32 {
        match n {
            1 | 2 => 1,
            n => fibonacci(n - 1) + fibonacci(n - 2)
        }
    }
}
// ...

    test("fibonacci", () => {
        expect(native.fibonacci(5)).toBe(5);
        expect(native.fibonacci(10)).toBe(55);
    })

// ...

@GabrielCastro
Copy link
Owner

Thanks this will for sure make lives easier

@GabrielCastro GabrielCastro reopened this Dec 11, 2017
@GabrielCastro GabrielCastro merged commit 391a0b4 into GabrielCastro:master Dec 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants