/*
 * Copyright LWJGL. All rights reserved.
 * License terms: https://www.lwjgl.org/license
 */
package org.lwjgl.system.dyncall;

import org.lwjgl.BufferUtils;
import org.lwjgl.system.*;
import org.testng.annotations.Test;

import java.nio.IntBuffer;

import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.system.dyncall.DynCall.*;
import static org.testng.Assert.*;

@Test
public class DynCallTest {

	/** Test MemoryAccess.putInt using dyncall. */
	public void testDynCall() {
		// Get the function address. Ignore this particular implementation, normally we'd create
		// a SharedLibrary instance here and call getFunctionAddress("<function name>").
		long putInt = getPutIntAddress();

		// The memory we'll modify using dyncall
		IntBuffer target = BufferUtils.createIntBuffer(1);

		// Create the CallVM object
		long vm = dcNewCallVM(1024);

		// Set calling convention
		dcMode(vm, DC_CALL_C_DEFAULT);

		{
			// must reset before every call
			dcReset(vm);

			// bind the arguments
			dcArgPointer(vm, NULL); // JNIEnv*
			dcArgPointer(vm, NULL); // jclass
			dcArgLongLong(vm, memAddress(target));
			dcArgInt(vm, 0xBAADF00D);

			// call the function
			dcCallVoid(vm, putInt);
		}

		// Validate
		assertEquals(target.get(0), 0xBAADF00D);

		// Free the CallVM object
		dcFree(vm);
	}

	private static long getPutIntAddress() {
		SharedLibrary lib = Library.loadNative(Library.JNI_LIBRARY_NAME);

		long putInt = lib.getFunctionAddress(
			Platform.get() == Platform.WINDOWS && Pointer.BITS32
				? "_Java_org_lwjgl_system_MemoryAccess_putInt@20" // __stdcall (Win32)
				: "Java_org_lwjgl_system_MemoryAccess_putInt"
		);

		assertTrue(putInt != NULL);

		return putInt;
	}

}
@@ -1352,16 +1352,22 @@
 s() %7B%0A%09%09
+try ( 
 SharedLi
@@ -1422,19 +1422,22 @@
 RY_NAME)
-;%0A%0A
+ ) %7B%0A%09
 %09%09long p
@@ -1471,196 +1471,55 @@
 ess(
-%0A%09%09%09Platform.get() == Platform.WINDOWS && Pointer.BITS32%0A%09%09%09%09? %22_Java_org_lwjgl_system_MemoryAccess_putInt@20%22 // __stdcall (Win32)%0A%09%09%09%09: %22Java_org_lwjgl_system_MemoryAccess_putInt%22%0A%09%09);%0A%0A
+%22Java_org_lwjgl_system_MemoryAccess_putInt%22);%0A%09
 %09%09as
@@ -1544,17 +1544,17 @@
  NULL);%0A
-%0A
+%09
 %09%09return
@@ -1562,13 +1562,17 @@
 putInt;%0A
+%09%09%7D%0A
 %09%7D%0A%0A%7D

Fix dyncall test