-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #20 from kelnishi/tail-calls
Tail calls
- Loading branch information
Showing
8 changed files
with
381 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// /* | ||
// * Copyright 2024 Kelvin Nishikawa | ||
// * | ||
// * Licensed under the Apache License, Version 2.0 (the "License"); | ||
// * you may not use this file except in compliance with the License. | ||
// * You may obtain a copy of the License at | ||
// * | ||
// * http://www.apache.org/licenses/LICENSE-2.0 | ||
// * | ||
// * Unless required by applicable law or agreed to in writing, software | ||
// * distributed under the License is distributed on an "AS IS" BASIS, | ||
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// * See the License for the specific language governing permissions and | ||
// * limitations under the License. | ||
// */ | ||
|
||
using System; | ||
using System.IO; | ||
using Wacs.Core; | ||
using Wacs.Core.Runtime; | ||
using Xunit; | ||
|
||
namespace Spec.Test | ||
{ | ||
public class ExtensionTests | ||
{ | ||
[Fact] | ||
public void TailCallFactorial() | ||
{ | ||
var runtime = new WasmRuntime(); | ||
|
||
using var fileStream = new FileStream("../../../engine/tailcalls.wasm", FileMode.Open); | ||
var module = BinaryModuleParser.ParseWasm(fileStream); | ||
var moduleInst = runtime.InstantiateModule(module); | ||
runtime.RegisterModule("tailcalls", moduleInst); | ||
|
||
var fa = runtime.GetExportedFunction(("tailcalls", "factorial")); | ||
var invoker = runtime.CreateInvoker<Func<long, long>>(fa); | ||
|
||
Assert.Equal(479_001_600, invoker(12)); | ||
Assert.Equal(2_432_902_008_176_640_000 , invoker(20)); | ||
} | ||
} | ||
} |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
(module | ||
;; Recursive factorial implementation using tail calls | ||
(func $fac (export "factorial") (param $x i64) (result i64) | ||
(return_call $fac-aux (local.get $x) (i64.const 1)) | ||
) | ||
(func $fac-aux (param $x i64) (param $r i64) (result i64) | ||
(if (result i64) (i64.eqz (local.get $x)) | ||
(then | ||
(local.get $r) | ||
) | ||
(else | ||
(return_call $fac-aux | ||
(i64.sub (local.get $x) (i64.const 1)) | ||
(i64.mul (local.get $x) (local.get $r)) | ||
) | ||
) | ||
) | ||
) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.