Challenge - 5 Problems
Deno FFI Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediateWhat does this Deno FFI code output?
Consider this Deno FFI snippet that calls a native C function to add two numbers. What will be printed?
Deno
const dylib = Deno.dlopen("./libadd.so", { add: { parameters: ["i32", "i32"], result: "i32" } }); const result = dylib.symbols.add(5, 7); console.log(result); dylib.close();
Attempts:
2 left
💡 Hint
Think about what the native function 'add' is expected to do with two integers.
✗ Incorrect
The native function 'add' takes two 32-bit integers and returns their sum. Calling add(5,7) returns 12, which is printed.
📝 Syntax
intermediateWhich option correctly defines a Deno FFI function signature for a C function returning a double?
You want to call a native C function that returns a double and takes a single float parameter. Which Deno FFI signature is correct?
Attempts:
2 left
💡 Hint
Deno FFI uses specific type strings like 'f32' and 'f64' for floats and doubles.
✗ Incorrect
In Deno FFI, 'f32' represents a 32-bit float parameter and 'f64' represents a 64-bit double return type. Option A matches this.
🔧 Debug
advancedWhy does this Deno FFI call throw a TypeError?
Given this code snippet, why does calling dylib.symbols.multiply(3, '4') throw a TypeError?
Deno
const dylib = Deno.dlopen("./libmath.so", { multiply: { parameters: ["i32", "i32"], result: "i32" } }); const result = dylib.symbols.multiply(3, '4'); console.log(result); dylib.close();
Attempts:
2 left
💡 Hint
Check the types of arguments passed versus the expected parameter types.
✗ Incorrect
The FFI expects two 32-bit integers. Passing a string '4' causes a TypeError because the type does not match.
🧠 Conceptual
advancedWhat is the purpose of the 'unsafe' keyword in Deno FFI function definitions?
In Deno FFI, some function definitions include the 'unsafe' flag. What does this flag indicate?
Attempts:
2 left
💡 Hint
Consider what 'unsafe' usually means in programming contexts related to memory and calls.
✗ Incorrect
The 'unsafe' flag marks functions that can cause crashes or undefined behavior if used incorrectly, disabling some runtime safety checks.
❓ state_output
expertWhat is the value of 'result' after this Deno FFI call sequence?
Given this code, what is the final value of 'result'?
Deno
const dylib = Deno.dlopen("./libcounter.so", { increment: { parameters: ["pointer"], result: "void" }, getValue: { parameters: ["pointer"], result: "i32" } }); const counter = new Uint32Array(1); counter[0] = 0; const ptr = Deno.UnsafePointer.of(counter.buffer); dylib.symbols.increment(ptr); dylib.symbols.increment(ptr); const result = dylib.symbols.getValue(ptr); dylib.close();
Attempts:
2 left
💡 Hint
The native 'increment' function increases the value pointed to by the pointer by 1 each call.
✗ Incorrect
The counter starts at 0. Two increments increase it to 2. Then getValue reads 2.
