why this code does not work?

Just starting out? Need help? Post your questions and find answers here.
kingwolf71
Posts: 21
Joined: Wed May 01, 2019 10:14 am

why this code does not work?

Post by kingwolf71 »

Code: Select all


EnableJS

function memcpy(dest, src, n) 
{
   dest.set(src.slice(0, n))
}
DisableJS

Procedure   test1()
   *p1 = 0
   *p2 = 0
   
   !p_p1 = new Uint8Array(100)
   !p_p2 = new Uint8Array(100)
   PokeS( *p1, 0, "today is a good day", -1 )
   PokeS( *p2, 0, "another test string", -1 )
   
   ;!memcpy( p_2, p_1, 10 )
   
   Debug PeekS( *p1, 0, -1 )
   Debug PeekS( *p2, 0, -1 )      
EndProcedure

Debug "Test 1 start"
test1()
Debug "Test 1 end"



I have been trying to make a memory copy routing in SB as Peek and Poke have proven to be ineffective. Any ideas?
User avatar
Peter
Posts: 1093
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: why this code does not work?

Post by Peter »

Code: Select all

EnableExplicit

Procedure CopyMemory(SourceMemory, DestinationMemory, Length)
  
  Protected Counter
  
  For Counter = 0 To Length - 1
    PokeB(DestinationMemory, Counter, PeekB(SourceMemory, Counter))
  Next
  
EndProcedure

Define *p1, *p2

*p1 = AllocateMemory(100)
*p2 = AllocateMemory(100)

PokeS(*p1, 0, "abcdefghijklmnop")
PokeS(*p2, 0, "0123456789")

Debug "Before CopyMemory():"
Debug "*p1: " + PeekS(*p1, 0)
Debug "*p2: " + PeekS(*p2, 0)

CopyMemory(*p2, *p1, 10)

Debug "---"
Debug "After CopyMemory():"
Debug "*p1: " + PeekS(*p1, 0)
Debug "*p2: " + PeekS(*p2, 0)
(Must still be tested extensively)
ljgww
Posts: 26
Joined: Thu Mar 26, 2020 5:47 pm

Re: why this code does not work?

Post by ljgww »

Unoptimized version that works.
This is debug version - some code can be removed.
There are several ways of elaboration on top of this subject (some of ideas where to go):

- version that do not use JS function
- version that introduce offsets
- version that works with different argument types (e.g. int, float etc.)
- perhaps even a macro (see wishlist forum on JS land adventures)

Code: Select all

Procedure MemCpy(*dest, *src, n)
EnableJS
function memcpy(dest, src, n)
{
destAr = new Uint8Array(dest);
srcAr = new Uint8Array(src);
console.log("Src:", srcAr);
console.log("Src slice:", srcAr.slice(0,n));
console.log("Dest:", destAr);
console.log("n:", n);
destAr.set(srcAr.slice(0,n),0);
console.log("Dest after:", destAr);
}
memcpy(p_dest, p_src, v_n);
DisableJS
EndProcedure


Procedure   test1()
  Define *p1, *p2
  *p1 = AllocateMemory(100)
  *p2 = AllocateMemory(100)
  
  PokeS( *p1, 0, "today is a good day", -1 )
  PokeS( *p2, 0, "insertinto test string", -1 )
  
  Debug "Before"
  Debug PeekS( *p1, 0, -1 )
  Debug PeekS( *p2, 0, -1 )     
  
  MemCpy(*p2, *p1, 10)
  
  Debug "After"
  Debug PeekS( *p1, 0, -1 )
  Debug PeekS( *p2, 0, -1 )     
  
EndProcedure

Debug "Test 1 start"
test1()
Debug "Test 1 end"
Output

Code: Select all

Test 1 start
Before
today is a good day
insertinto test string
After
today is a good day
today is a test string
Test 1 end
Post Reply