Skip to content

Commit 62f68db

Browse files
committed
c++
1 parent 2eb4b19 commit 62f68db

File tree

9 files changed

+1791
-1
lines changed

9 files changed

+1791
-1
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.vscode/c_cpp_properties.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"configurations": [
3+
{
4+
"name": "Mac",
5+
"includePath": [
6+
"${workspaceFolder}/**"
7+
],
8+
"defines": [],
9+
"macFrameworkPath": []
10+
}
11+
],
12+
"version": 4
13+
}

.vscode/launch.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"skipFiles": [
1212
"<node_internals>/**"
1313
],
14-
"program": "${workspaceFolder}/15-http/150-raw-request.js"
14+
"program": "${workspaceFolder}/27-request-showdown/270-request-http.js"
1515
}
1616
]
1717
}

28-isprime.cpp/280-cppaddon-prime.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#include <napi.h> //import the napi header
2+
3+
// Function to check if a number is prime
4+
//native simple c++ (Node can't see this yet)
5+
bool IsPrime(int number) {
6+
if (number <= 1) return false;
7+
for (int i = 2; i * i <= number; ++i) {
8+
if (number % i == 0) return false;
9+
}
10+
return true;
11+
}
12+
13+
// N-API wrapper function
14+
//need to wrap it so node can see it and call it
15+
//and yes we need a special call back so we can call JS back
16+
Napi::Boolean IsPrimeWrapped(const Napi::CallbackInfo& info) {
17+
Napi::Env env = info.Env();
18+
19+
// Ensure there is one argument and it is a number
20+
if (info.Length() < 1 || !info[0].IsNumber()) {
21+
Napi::TypeError::New(env, "Number expected").ThrowAsJavaScriptException();
22+
return Napi::Boolean::New(env, false);
23+
}
24+
25+
int number = info[0].As<Napi::Number>().Int32Value();
26+
bool result = IsPrime(number);
27+
28+
return Napi::Boolean::New(env, result);
29+
}
30+
31+
// Initialize the module and export functions
32+
Napi::Object Init(Napi::Env env, Napi::Object exports) {
33+
exports.Set(Napi::String::New(env, "isPrime"),
34+
Napi::Function::New(env, IsPrimeWrapped));
35+
return exports;
36+
}
37+
38+
NODE_API_MODULE(prime_checker, Init)

28-isprime.cpp/binding.gyp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "prime_checker",
5+
"sources": ["280-cppaddon-prime.cpp"]
6+
}
7+
]
8+
}

0 commit comments

Comments
 (0)