Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
W
wotb-rs
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Container registry
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nodes
typescript
wotb-rs
Commits
f4586b2f
Commit
f4586b2f
authored
7 years ago
by
nanocryk
Browse files
Options
Downloads
Plain Diff
Merge branch 'feature/neon-wrapper' into develop
parents
bf28fd74
8bd6c6ce
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
.gitignore
+4
-0
4 additions, 0 deletions
.gitignore
lib/index.js
+19
-2
19 additions, 2 deletions
lib/index.js
native/src/lib.rs
+272
-6
272 additions, 6 deletions
native/src/lib.rs
with
295 additions
and
8 deletions
.gitignore
+
4
−
0
View file @
f4586b2f
...
@@ -4,3 +4,7 @@ native/artifacts.json
...
@@ -4,3 +4,7 @@ native/artifacts.json
**/*~
**/*~
**/node_modules
**/node_modules
**/.DS_Store
**/.DS_Store
*.bk
*.wot
This diff is collapsed.
Click to expand it.
lib/index.js
+
19
−
2
View file @
f4586b2f
var
addon
=
require
(
'
../native
'
);
let
WebOfTrust
=
require
(
'
../native
'
)
.
WebOfTrust
;
console
.
log
(
addon
.
hello
());
{
let
wot
=
new
WebOfTrust
(
3
);
console
.
log
(
wot
.
getMaxCert
())
wot
.
setMaxCert
(
4
);
console
.
log
(
wot
.
getMaxCert
())
console
.
log
(
wot
.
addNode
());
console
.
log
(
wot
.
getWoTSize
())
}
console
.
log
(
"
-----
"
)
{
let
wot
=
new
WebOfTrust
(
"
hey.wot
"
);
console
.
log
(
wot
.
getMaxCert
())
console
.
log
(
wot
.
addNode
());
console
.
log
(
wot
.
getWoTSize
());
console
.
log
(
wot
.
toFile
(
"
hey.wot
"
));
}
This diff is collapsed.
Click to expand it.
native/src/lib.rs
+
272
−
6
View file @
f4586b2f
//! `duniter-rs-wotb-js` is a crate providing Javascript bindings of `duniter-rs-wotb`.
#[macro_use]
#[macro_use]
extern
crate
neon
;
extern
crate
neon
;
extern
crate
duniter_rs_wotb
;
use
neon
::
js
::{
Value
,
JsInteger
,
JsString
,
JsBoolean
,
JsFunction
,
JsArray
,
JsNumber
,
JsObject
,
Object
};
use
neon
::
js
::
class
::{
Class
,
JsClass
};
use
neon
::
mem
::
Handle
;
use
neon
::
vm
::{
Throw
,
Lock
};
use
duniter_rs_wotb
::{
WebOfTrust
,
NodeId
,
NewLinkResult
,
RemovedLinkResult
};
declare_types!
{
/// JS class wrapping WebOfTrust struct.
pub
class
JsWebOfTrust
for
WebOfTrust
{
init
(
call
)
{
let
scope
=
call
.scope
;
let
arg0
=
try!
(
call
.arguments
.require
(
scope
,
0
));
if
let
Some
(
max_cert
)
=
arg0
.downcast
::
<
JsInteger
>
()
{
let
max_cert
=
max_cert
.value
();
match
max_cert
>
0
{
true
=>
Ok
(
WebOfTrust
::
new
(
max_cert
as
usize
)),
false
=>
Err
(
Throw
),
}
}
else
if
let
Some
(
path
)
=
arg0
.downcast
::
<
JsString
>
()
{
let
path
=
path
.value
();
match
WebOfTrust
::
from_file
(
path
.as_str
())
{
Some
(
wot
)
=>
Ok
(
wot
),
None
=>
Err
(
Throw
),
}
}
else
{
Err
(
Throw
)
}
}
method
toFile
(
call
)
{
let
scope
=
call
.scope
;
let
path
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.to_string
(
scope
))
.value
();
let
result
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.to_file
(
path
.as_str
())
});
Ok
(
JsBoolean
::
new
(
scope
,
result
)
.upcast
())
}
method
addNode
(
call
)
{
let
scope
=
call
.scope
;
let
id
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.add_node
()
});
Ok
(
JsInteger
::
new
(
scope
,
*
id
as
i32
)
.upcast
())
}
method
removeNode
(
call
)
{
let
scope
=
call
.scope
;
let
id
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
match
wot
.remove_node
()
{
Some
(
id
)
=>
*
id
as
i32
,
None
=>
-
1
,
}
});
Ok
(
JsInteger
::
new
(
scope
,
id
)
.upcast
())
}
method
getSentries
(
call
)
{
let
scope
=
call
.scope
;
let
arg0
=
try!
(
call
.arguments
.require
(
scope
,
0
));
let
d_min
=
try!
(
arg0
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
array
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.get_sentries
(
d_min
)
});
let
jsarray
:
Handle
<
JsArray
>
=
JsArray
::
new
(
scope
,
array
.len
()
as
u32
);
for
(
index
,
&
item
)
in
array
.iter
()
.enumerate
()
{
try!
(
jsarray
.set
(
index
as
u32
,
JsInteger
::
new
(
scope
,
*
item
as
i32
)));
}
Ok
(
jsarray
.upcast
())
}
method
getNonSentries
(
call
)
{
let
scope
=
call
.scope
;
let
arg0
=
try!
(
call
.arguments
.require
(
scope
,
0
));
let
d_min
=
try!
(
arg0
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
array
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.get_non_sentries
(
d_min
)
});
let
jsarray
:
Handle
<
JsArray
>
=
JsArray
::
new
(
scope
,
array
.len
()
as
u32
);
for
(
index
,
&
item
)
in
array
.iter
()
.enumerate
()
{
try!
(
jsarray
.set
(
index
as
u32
,
JsInteger
::
new
(
scope
,
*
item
as
i32
)));
}
Ok
(
jsarray
.upcast
())
}
method
getDisabled
(
call
)
{
let
scope
=
call
.scope
;
let
array
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.get_disabled
()
});
let
jsarray
:
Handle
<
JsArray
>
=
JsArray
::
new
(
scope
,
array
.len
()
as
u32
);
for
(
index
,
&
item
)
in
array
.iter
()
.enumerate
()
{
try!
(
jsarray
.set
(
index
as
u32
,
JsInteger
::
new
(
scope
,
*
item
as
i32
)));
}
Ok
(
jsarray
.upcast
())
}
method
getPaths
(
call
)
{
let
scope
=
call
.scope
;
let
from
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
to
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
k_max
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
2
))
.check
::
<
JsInteger
>
())
.value
()
as
u32
;
use
neon
::
vm
::{
Call
,
JsResult
};
let
paths
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
use
neon
::
js
::
JsString
;
wot
.get_paths
(
NodeId
(
from
),
NodeId
(
to
),
k_max
)
});
let
jsarray
:
Handle
<
JsArray
>
=
JsArray
::
new
(
scope
,
paths
.len
()
as
u32
);
for
(
index
,
ref
inner_array
)
in
paths
.iter
()
.enumerate
()
{
let
inner_jsarray
:
Handle
<
JsArray
>
=
JsArray
::
new
(
scope
,
inner_array
.len
()
as
u32
);
for
(
inner_index
,
&
item
)
in
inner_array
.iter
()
.enumerate
()
{
try!
(
inner_jsarray
.set
(
inner_index
as
u32
,
JsInteger
::
new
(
scope
,
*
item
as
i32
)));
}
try!
(
jsarray
.set
(
index
as
u32
,
inner_jsarray
));
}
Ok
(
jsarray
.upcast
())
}
method
getWoTSize
(
call
)
{
let
scope
=
call
.scope
;
let
size
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.size
()
});
Ok
(
JsInteger
::
new
(
scope
,
size
as
i32
)
.upcast
())
}
method
isEnabled
(
call
)
{
let
scope
=
call
.scope
;
let
node
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
match
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.is_enabled
(
NodeId
(
node
))
})
{
Some
(
state
)
=>
Ok
(
JsBoolean
::
new
(
scope
,
state
)
.upcast
()),
None
=>
Err
(
Throw
),
}
}
method
setEnabled
(
call
)
{
let
scope
=
call
.scope
;
let
node
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
state
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsBoolean
>
())
.value
();
match
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.set_enabled
(
NodeId
(
node
),
state
)
})
{
Some
(
state
)
=>
Ok
(
JsBoolean
::
new
(
scope
,
state
)
.upcast
()),
None
=>
Err
(
Throw
),
}
}
method
addLink
(
call
)
{
let
scope
=
call
.scope
;
let
from
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
to
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
match
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.add_link
(
NodeId
(
from
),
NodeId
(
to
))
})
{
NewLinkResult
::
Ok
(
count
)
|
NewLinkResult
::
AlreadyCertified
(
count
)
|
NewLinkResult
::
AllCertificationsUsed
(
count
)
=>
Ok
(
JsInteger
::
new
(
scope
,
count
as
i32
)
.upcast
()),
_
=>
Err
(
Throw
),
}
}
method
removeLink
(
call
)
{
let
scope
=
call
.scope
;
let
from
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
to
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
match
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.remove_link
(
NodeId
(
from
),
NodeId
(
to
))
})
{
RemovedLinkResult
::
Removed
(
count
)
=>
Ok
(
JsInteger
::
new
(
scope
,
count
as
i32
)
.upcast
()),
_
=>
Err
(
Throw
),
}
}
method
existsLink
(
call
)
{
let
scope
=
call
.scope
;
let
from
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
to
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
result
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.exists_link
(
NodeId
(
from
),
NodeId
(
to
))
});
Ok
(
JsBoolean
::
new
(
scope
,
result
)
.upcast
())
}
method
isOutdistanced
(
call
)
{
let
scope
=
call
.scope
;
fn
hello
(
call
:
Call
)
->
JsResult
<
JsString
>
{
let
node
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
let
d_min
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
1
))
.check
::
<
JsInteger
>
())
.value
()
as
u32
;
let
d_max
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
2
))
.check
::
<
JsInteger
>
())
.value
()
as
u32
;
let
x_percent
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
3
))
.check
::
<
JsNumber
>
())
.value
()
as
f64
;
match
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.is_outdistanced
(
NodeId
(
node
),
d_min
,
d_max
,
x_percent
)
})
{
Some
(
result
)
=>
Ok
(
JsBoolean
::
new
(
scope
,
result
)
.upcast
()),
None
=>
Err
(
Throw
),
}
}
method
setMaxCert
(
call
)
{
let
scope
=
call
.scope
;
let
max_cert
=
try!
(
try!
(
call
.arguments
.require
(
scope
,
0
))
.check
::
<
JsInteger
>
())
.value
()
as
usize
;
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.max_cert
=
max_cert
;
});
Ok
(
JsObject
::
new
(
scope
)
.upcast
())
}
method
getMaxCert
(
call
)
{
let
scope
=
call
.scope
;
let
scope
=
call
.scope
;
Ok
(
JsString
::
new
(
scope
,
"hello node"
)
.unwrap
())
let
max_cert
=
call
.arguments
.this
(
scope
)
.grab
(|
wot
|
{
wot
.max_cert
});
Ok
(
JsInteger
::
new
(
scope
,
max_cert
as
i32
)
.upcast
())
}
}
}
}
register_module!
(
m
,
{
register_module!
(
m
,
{
m
.export
(
"hello"
,
hello
)
let
class
:
Handle
<
JsClass
<
JsWebOfTrust
>>
=
try!
(
JsWebOfTrust
::
class
(
m
.scope
));
let
constructor
:
Handle
<
JsFunction
<
JsWebOfTrust
>>
=
try!
(
class
.constructor
(
m
.scope
));
try!
(
m
.exports
.set
(
"WebOfTrust"
,
constructor
));
Ok
(())
});
});
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment