Skip to content

Commit

Permalink
undo renaming to prevent links from breaking when order changes, add …
Browse files Browse the repository at this point in the history
…classes example
  • Loading branch information
amilajack committed Nov 9, 2018
1 parent bcdbe0f commit 8403600
Show file tree
Hide file tree
Showing 56 changed files with 930 additions and 4 deletions.
3 changes: 0 additions & 3 deletions 1_hello-world/lib/index.js

This file was deleted.

12 changes: 12 additions & 0 deletions README.md
Expand Up @@ -2,6 +2,18 @@

[![Build Status](https://travis-ci.com/amilajack/examples.svg?branch=master)](https://travis-ci.com/amilajack/examples)

## Table of Contents

1. [hello world](https://github.com/amilajack/neon-examples/tree/master/hello-world)
2. [electron app]((https://github.com/amilajack/neon-examples/tree/master/electron-app))
3. [word counting](https://github.com/amilajack/neon-examples/tree/master/word-counting)
4. [bigint task]((https://github.com/amilajack/neon-examples/tree/master/bigint-task))
5. [binary](https://github.com/amilajack/neon-examples/tree/master/binary)
6. [json]((https://github.com/amilajack/neon-examples/tree/master/json))
7. [json]((https://github.com/amilajack/neon-examples/tree/master/classes))

## Setup

```bash
git clone https://github.com/amilajack/examples
cd examples
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 3 additions & 0 deletions classes/README.md
@@ -0,0 +1,3 @@
# classes

A simple example of a Neon class.
4 changes: 4 additions & 0 deletions classes/lib/index.js
@@ -0,0 +1,4 @@
var addon = require('../native');

exports.Greeter = addon.Greeter;
exports.Uncallable = addon.Uncallable;
150 changes: 150 additions & 0 deletions classes/native/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions classes/native/Cargo.toml
@@ -0,0 +1,12 @@
[package]
name = "neon-class-example"
version = "0.0.1"
authors = ["Dave Herman <dherman@mozilla.com>"]
license = "MIT"

[lib]
name = "neon_class_example"
crate-type = ["dylib"]

[dependencies]
neon = "0.2"
80 changes: 80 additions & 0 deletions classes/native/src/lib.rs
@@ -0,0 +1,80 @@
#[macro_use]
extern crate neon;

use neon::prelude::*;

pub struct User {
id: i32,
first_name: String,
last_name: String,
email: String,
}

type Unit = ();

declare_types! {
pub class JsUser for User {
init(mut cx) {
let id = cx.argument::<JsNumber>(0)?;
let first_name: Handle<JsString> = cx.argument::<JsString>(1)?;
let last_name: Handle<JsString> = cx.argument::<JsString>(2)?;
let email: Handle<JsString> = cx.argument::<JsString>(3)?;

Ok(User {
id: id.value() as i32,
first_name: first_name.value(),
last_name: last_name.value(),
email: email.value(),
})
}

method get(mut cx) {
let attr: String = cx.argument::<JsString>(0)?.value();

let this = cx.this();

match &attr[..] {
"id" => {
let id = {
let guard = cx.lock();
let user = this.borrow(&guard);
user.id
};
Ok(cx.number(id).upcast())
},
"first_name" => {
let first_name = {
let guard = cx.lock();
let user = this.borrow(&guard);
user.first_name.clone()
};
Ok(cx.string(&first_name).upcast())
},
"last_name" => {
let last_name = {
let guard = cx.lock();
let user = this.borrow(&guard);
user.last_name.clone()
};
Ok(cx.string(&last_name).upcast())
},
"email" => {
let email = {
let guard = cx.lock();
let user = this.borrow(&guard);
user.email.clone()
};
Ok(cx.string(&email).upcast())
},
_ => cx.throw_type_error("property does not exist")
}
}

method panic(_) {
panic!("User.prototype.panic")
}
}
}
register_module!(mut cx, {
cx.export_class::<JsUser>("User")
});

0 comments on commit 8403600

Please sign in to comment.