𝗝𝗮𝗺𝗲𝘀
𝗝𝗮𝗺𝗲𝘀
CC#
Created by 𝗝𝗮𝗺𝗲𝘀 on 3/28/2025 in #help
Cant import scoped Javascript in Blazor Standalone WebAssembly
In the past I have already done this in InteractiveServer Blazor, which is to import {page}.razor.js in my razor page through IJSRuntime in the OnAfterRender step of the page lifecycle. as follows: Page.Razor.cs
public partial class Page : ComponentBase
{
[Parameter] public required IJSRuntime js { get; init; }
private IJSObjectReference pageModule;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
pageModule = js.InvokeAsync<IJSObjectReference>("import"
, "Components/Pages/Page.razor.js")
}
}
// dispose pattern hidden for simplicity
}
public partial class Page : ComponentBase
{
[Parameter] public required IJSRuntime js { get; init; }
private IJSObjectReference pageModule;

protected override async Task OnAfterRenderAsync(bool firstRender)
{
if (firstRender)
{
pageModule = js.InvokeAsync<IJSObjectReference>("import"
, "Components/Pages/Page.razor.js")
}
}
// dispose pattern hidden for simplicity
}
Page.razor.js
export function greet()
{
window.alert('!');
}
export function greet()
{
window.alert('!');
}
There is a way to make something similar work, by using global javascript files in wwwroot\*.js and <script... directives but that is not desired in the moment. I didn't find any way to use a modular aproach handled by the ComponentBase object. This is the web assembly startup implementation Program.cs
using BlazorApp2;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
using BlazorApp2;
using Microsoft.AspNetCore.Components.Web;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;

var builder = WebAssemblyHostBuilder.CreateDefault(args);
builder.RootComponents.Add<App>("#app");
builder.RootComponents.Add<HeadOutlet>("head::after");

builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) });

await builder.Build().RunAsync();
.NET Core 9 environment
// stack overflow post: https://stackoverflow.com/questions/79542079/cant-import-scoped-javascript-in-blazor-standalone-webassembly
3 replies
CC#
Created by 𝗝𝗮𝗺𝗲𝘀 on 9/18/2024 in #help
Does structs generate inaccessible copies?
Consider the following large structure for the asked questions
[InlineArray(128)]
public struct x1024BitBuffer
{
byte e0;
}
[InlineArray(128)]
public struct x1024BitBuffer
{
byte e0;
}
Does a local copy is created when returning from a method? In my understanding of how structure returning methods are handled, the current method will allocate the necessary memory to store the called method's return within the stack. But, the called method also has it's own stack, so, in the next scenarios, does the struct gets copied back in forth or is it internally passed by reference?
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
=> new()
;

static void Main()
{
var x = CreateMe();
}
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
=> new()
;

static void Main()
{
var x = CreateMe();
}
When Main invokes CreateMe is the structure allocated inside the creator method or does the compiler detects that no one can access it so it makes new() allocate directly into x's var memory? What if the variable is accessed in the creator method but only returned
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
{
var temp = new x1024BitBuffer ();
temp[5] = 0x11;
return temp;
}

static void Main()
{
var x = CreateMe();
}
// consider that we marked this method NOT to be inlined
static x1024BitBuffer CreateMe()
{
var temp = new x1024BitBuffer ();
temp[5] = 0x11;
return temp;
}

static void Main()
{
var x = CreateMe();
}
Since the value temp is only modified and then returned, is the memory for it allocated in CreateMe's stack and then copied to the caller method stack or accessed directly? Does a a copy of a struct is created when passing through an init accessor?
readonly struct Data
{
readonly x1024BitBuffer payload;

public x1024BitBuffer Payload
{
init => payload = value;
}
}

static void Main()
{
Data data = new()
{
PayLoad = CreateMe()
};
}
readonly struct Data
{
readonly x1024BitBuffer payload;

public x1024BitBuffer Payload
{
init => payload = value;
}
}

static void Main()
{
Data data = new()
{
PayLoad = CreateMe()
};
}
When creating a Data instance, the IL method init_Payload is called. So the stack traces looks something like the following .With that in mind, does the large structure gets copied 2 times or the compiler optmises that in some way? Main() -> CreateMe() -> init_Payload new Data() | => new(); | arg: value
12 replies
CC#
Created by 𝗝𝗮𝗺𝗲𝘀 on 5/7/2024 in #help
Are streams any better than byte array outside of I.O context?
Streams are visibly better when working with files, network responses and many other cases, but, when specifically working with objects which already live within RAM memory, are streams still useful? I.E why does Binary Formatter used streams when formatting managed objects? The understanding that I have from streams is a lot like IEnumerable<byte> with a temp buffer, meaning, nothing happens upon creation until fetched. Maybe postpone an action till last moment?
6 replies